How to get user's IP address in Laravel?

0 votes
How to know the ip address of the user in laravel those who have visited my website?
Mar 20 in Laravel by kartik
• 27,360 points
5,562 views
How can i know the exact location through ip using laravel??help me

4 answers to this question.

0 votes

Hey,

We can get the user's IP address using:

public function getUserIp(Request $request){  

            // Gettingip address of remote user  

            return $user_ip_address=$request->ip();  

}  

Hope this will be helpful!

Thank you!!

answered Mar 20 by Niroj
• 58,960 points
Thank you sm...it worked foe me

Can u solve my other problem that when i use request object it doesn't work for me, it returns192.168.10.10 which is obviously not my IP address the address of my Homestead server.how to solve this??

Hello @Shivani,

If you are under a load balancer, Laravel's \Request::ip() always returns the balancer's IP:

            echo $request->ip();
            // server ip

            echo \Request::ip();
            // server ip

            echo \request()->ip();
            // server ip

            echo $this->getIp(); //see the method below
            // clent ip

This custom method returns the real client ip:

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;
                }
            }
        }
    }
}

In addition to this I suggest you to be very careful using Laravel's throttle middleware: It uses Laravel's Request::ip() as well, so all your visitors will be identified as the same user and you will hit the throttle limit very quickly. I experienced this live and this caused big issues.

To fix this:

Illuminate\Http\Request.php

    public function ip()
    {
        //return $this->getClientIp(); //original method
        return $this->getIp(); // the above method
    }

You can now also use Request::ip(), which should return the real IP in production.

Hope it helps!!
Thank you!!

Many thanks to you

if had a vulnerability it takes HTTP_X_FORWARDED_FOR as input which allows non-admins to gain admin access by altering the HTTP_X_FORWARDED_FOR header. Also see blog.ircmaxell.com/2012/11/anatomy-of-attack-how-i-hacked.html

Yes, it does. Otherwise you have a very broken server configuration.

0 votes
$ip = trim(shell_exec("dig +short myip.opendns.com @resolver1.opendns.com"));
answered Aug 28 by Jonjie
0 votes

Hello folks

There are two things to take care of:

  1. Get a helper function that returns a Illuminate\Http\Request and call the ->ip() method:

    request()->ip();
  2. Think of your server configuration, it may use a proxy or load-balancer, especially in an AWS ELB configuration.

If this is your case you need to follow "Configuring Trusted Proxies" or maybe even set a "Trusting All Proxies" option.

answered Sep 16 by Nikhil
• 140 points
0 votes
Use $clientIP = request()->ip();
answered Sep 21 by aakash
• 210 points

Related Questions In Laravel

0 votes
1 answer

How to get distinct values for non-key column fields in Laravel?

Hello @kartik In eloquent you can use this $users ...READ MORE

answered Mar 30 in Laravel by Niroj
• 58,960 points
3,758 views
0 votes
1 answer

How to get a list of registered route paths in Laravel?

Hello, Route::getRoutes() returns a RouteCollection. On each element, you can ...READ MORE

answered Mar 31 in Laravel by Niroj
• 58,960 points
211 views
0 votes
1 answer

How to get all pending jobs in laravel queue on redis?

Hello @kartik, Here is the way I do ...READ MORE

answered Apr 6 in Laravel by Niroj
• 58,960 points
1,077 views
0 votes
1 answer

How to get data from Laravel backend and display in Vue/Nuxt frontend

Hello, You have to use the package dotenv. Then ...READ MORE

answered Apr 8 in Laravel by Niroj
• 58,960 points
585 views
0 votes
1 answer

How to download and install Lavavel framework?

Hey @kartik, First you must have xampp install ...READ MORE

answered Mar 17 in Laravel by Niroj
• 58,960 points
172 views
0 votes
1 answer

Display Laravel in browser by using cmd promt?

Hello, First you need to have laravel install ...READ MORE

answered Mar 17 in Laravel by Niroj
• 58,960 points
67 views
0 votes
1 answer

How can we get started with Laravel through Xampp?

Hii, First you need to start Apache and ...READ MORE

answered Mar 17 in Laravel by Niroj
• 58,960 points
74 views
0 votes
1 answer

How to change Laravel official name to any customize name?

Hey, You just need to go Laravel folder through ...READ MORE

answered Mar 17 in Laravel by Niroj
• 58,960 points
81 views
0 votes
1 answer

How to get client IP address in Laravel >5?

Hello @kartik, Use request()->ip(). Since Laravel 5 it's advised/good practice ...READ MORE

answered Sep 11 in Laravel by Niroj
• 58,960 points
26 views
0 votes
1 answer

How to identify wheather the request is HTTP GET or HTTP POST in Laravel?

Hey, In order to identify the type of ...READ MORE

answered Mar 19 in Laravel by Niroj
• 58,960 points
93 views