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 How to Change Date Format in Json Response Laravel

How to Change Date Format in Json Response Laravel ?

July 9, 2022November 17, 2023

In json response we get different data format and to change date format in Json Response Laravel we need to make bit extra efforts to show correct date format. Laravel provides by default two timestamp created_at and updated_at, and there format are according to database format but sometimes we want…

Read More
Php How to Delete Cookies in Laravel

How to Delete Cookies in Laravel ?

July 26, 2022July 27, 2022

In this article we will learn to delete cookies in laravel. In our last article we learnt to set and get cookies and how cookies are used to store the data in client computer, Cookie can hold tiny information in computer and can retrieve when required for same website. In…

Read More
Php How to fetch Soft Deleted Records in Laravel 9

How to Fetch Soft Deleted Records in Laravel ?

June 17, 2022June 21, 2022

In this article we will learn to fetch soft deleted records in Laravel. In our recent article Use Soft Delete to Temporary (Trash) Delete the Records in Laravel ? we learnt to delete the file without actually deleting from database and sometimes we want to show records that are soft…

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

  • 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 Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version