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

How to handle TokenMismatchException Ajax in Laravel 8 ?

December 30, 2021December 30, 2021

Laravel by default protects the application from unauthorized commands executed from outside the application means a suspicious user wanted to perform form submission from external command. But Laravel by default creates a token for every post request which we need to verify before reaching to out application logic. TokenMismatchException is…

Read More

How to print database query in laravel 8 ?

January 1, 2022February 22, 2024

Sometime to debug the MySQL query in laravel eloquent we need to print the raw MySQL query. In laravel we have multiple methods to print database query in laravel 8 and we can also print the query before executing it. Laravel eloquent method to print the query The first approach…

Read More
Php How to Check folder exist and Create a Nested Folder in Laravel

How to Check folder exist and Create Nested Folder in Laravel ?

May 24, 2022May 26, 2022

This article will guide you to Check folder exist and Create Nested Folder in Laravel. To perform the file system based operations like Check folder exist and Create a Nested Folder. we use File class in laravel. Laravel provides inbuilt library to access the file system and we can do…

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