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 Laravel multiple orderBy clause with example

Laravel multiple orderBy clause with example

October 6, 2022March 16, 2024

In this blog post, we will take a look at how to use the Laravel multiple orderBy clause in the Eloquent ORM. You can order query results by multiple columns using the orderBy method. This is useful when you want to sort by multiple criteria. In this post, we’ll show…

Read More
Php How to ShareSend Message to WhatsApp in Laravel PHP

How to Share/Send Message to WhatsApp in Laravel PHP ?

March 16, 2022March 16, 2022

Sharing or sending message to whatsapp from a website using a link is simple as possible. In this article we will not use any package or third party tool to share the url or message to whatsapp. Whatsapp itself provide to share or send message using the url so in…

Read More
Php Laravel custom login and register

Laravel 8 Custom login and registration with example

December 3, 2021December 3, 2021

In Laravel 8 there is multiple ways to implement the login and registration like Laravel provides its own auth with packages Jetstream, passport,sanctum, breeze and fortify. These all packages are easy to install and configure but sometimes our application requirement and design patterns are different or we can say we…

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