Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Get User IP Address in Laravel

How to Get User IP Address in Laravel ?

Aman Jain, June 24, 2022June 24, 2022

In this article we will learn to get user IP address in laravel. Client ip address is useful when we want to log the user request with ip address or wanted to know the location of user accessing our website. So in this article we will learn to use laravel inbuilt feature to get the client or user ip address.

You can use this example in any laravel 5, laravel 6, laravel 7, laravel 8 and also in laravel 9.

In php we need to do lots of stuff to fetch the real ip address of user but laravel we can fetch using request object as follow

request()->ip();  

//or

\Request::ip();

So we can understand this with multiple examples as follow

Method 1: Get User IP address using global request function

In this method we will get the ip address using request() global function of laravel

<?php
  namespace App\Http\Controllers;
use Illuminate\Http\Request;
  
class UserController extends Controller
{
     
    public function index(Request $request)
    {
    	  $userIp = $request->ip();   
        dd($userIp);
    }
}

Method 2: Get User IP Address using Custom Method

Sometimes when we are behind the load balancer then we will only get the server load balancer ip but we can get client ip using below method

Also Read : How to create custom logs file in laravel 8 / 9 ?

<?php
  namespace App\Http\Controllers;
use Illuminate\Http\Request;
  
class UserController extends Controller
{
     
    public function index(Request $request)
    {
    	  $userIp = $this->getIp();   
        dd($userIp);
    }
    public function getIp(){
    foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){
        if (array_key_exists($key, $_SERVER) === true){
            foreach (explode(',', $_SERVER[$key]) as $ip){
                $ip = trim($ip); // just to be safe
                if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
                    return $ip;
                }
            }
        }
    }
    return request()->ip(); // it will return server ip when no client ip found
    }
}

Method 3: Get User IP address using Request Class

In this method we will get the ip address using request() global function of laravel

<?php
  namespace App\Http\Controllers;
use Illuminate\Http\Request;
  
class UserController extends Controller
{
     
    public function index(Request $request)
    {
    	  $userIp = Request::ip(); //  $request->ip(); 
        dd($userIp);
    }
}

Related

Php Laravel Laravel 9 clientip addresslaravel

Post navigation

Previous post
Next post

Related Posts

Php Google Map with Multiple Marker and Info Box in Laravel

Google Map with Multiple Marker and Info Box in Laravel

August 30, 2022March 16, 2024

In this article we will learn to integrate google map with multiple marker and info box in laravel. Google maps are used to navigate the user location or indicate the location of business and any user. Sometimes In our application we want to show the multiple location with some information…

Read More
Php How to Add Default Value to Column in Laravel Migration

How to Add Default Value to Column in Laravel Migration?

August 17, 2022March 16, 2024

Default value to column is useful when we want to auto fill the default defined value of column during insertion of other values of table So In our this article i will show you to add default value to column in laravel migration. We can add any type of data…

Read More
Php upload file in php

How to upload file in php ?

September 13, 2021September 29, 2021

In this tutorial, we will learn basic of uploading of a file in php and also basic configuration required for php file upload. Configure the php.ini for large file upload By default php support 2 mb of file upload but in real world we require more than 2 mb. Same…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version