How to get client IP address in Laravel 5+?

I am trying to get the client IP address in Laravel. As we all know, it is much easier to get the client IP address in PHP using $_SERVER["REMOTE_ADDR"] .

It works fine mostly in PHP, but when I use the same in Laravel, it gives the server IP instead of the visitor's IP.

+103
php laravel laravel-5
Oct 21 '15 at 20:32
source share
13 answers

Looking at the Laravel API :

 Request::ip(); 

Internally, it uses the getClientIps method from the Symfony request object :

 public function getClientIps() { $clientIps = array(); $ip = $this->server->get('REMOTE_ADDR'); if (!$this->isFromTrustedProxy()) { return array($ip); } if (self::$trustedHeaders[self::HEADER_FORWARDED] && $this->headers->has(self::$trustedHeaders[self::HEADER_FORWARDED])) { $forwardedHeader = $this->headers->get(self::$trustedHeaders[self::HEADER_FORWARDED]); preg_match_all('{(for)=("?\[?)([a-z0-9\.:_\-/]*)}', $forwardedHeader, $matches); $clientIps = $matches[3]; } elseif (self::$trustedHeaders[self::HEADER_CLIENT_IP] && $this->headers->has(self::$trustedHeaders[self::HEADER_CLIENT_IP])) { $clientIps = array_map('trim', explode(',', $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_IP]))); } $clientIps[] = $ip; // Complete the IP chain with the IP the request actually came from $ip = $clientIps[0]; // Fallback to this when the client IP falls into the range of trusted proxies foreach ($clientIps as $key => $clientIp) { // Remove port (unfortunately, it does happen) if (preg_match('{((?:\d+\.){3}\d+)\:\d+}', $clientIp, $match)) { $clientIps[$key] = $clientIp = $match[1]; } if (IpUtils::checkIp($clientIp, self::$trustedProxies)) { unset($clientIps[$key]); } } // Now the IP chain contains only untrusted proxies and the client IP return $clientIps ? array_reverse($clientIps) : array($ip); } 
+143
Oct 21 '15 at 20:35
source share

Use request()->ip()

Starting with Laravel 5 (as far as I understand) it is recommended / recommended to use global functions, such as:

 response()->json($v); view('path.to.blade'); redirect(); route(); cookie(); 

You understand :-) And, if anything, when using functions (instead of a static notary), my IDE does not glow like a Christmas tree ;-)

+56
Dec 10 '15 at 15:00
source share

If you are under load balancer

Laravel \Request::ip() always returns the balancer 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 IP address of the client:

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



Read More: If you are using Laravel throttle middleware

In addition to this, I suggest that you be very careful when using the Laravel middleware throttle : It uses Laravel Request::ip() , so that all your visitors will be identified as the same user, and you will very quickly hit the throttle limit. Experience in life ... it led me to big problems ...

To fix this:

Lighten \ Http \ Request.php

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

Now you can also use Request::ip() , which should return a real IP in production

+51
Jan 20 '17 at 17:44
source share

Add namespace

 use Request; 

Then call the function

 Request::ip(); 
+23
Jan 19 '16 at 9:42 on
source share

For Laravel 5, you can use the Request object. Just call its ip () method. Something like:

$request->ip();

+13
Mar 07 '16 at 14:32
source share

In Laravel 5

 public function index(Request $request) { $request->ip(); } 
+10
Jun 21 '16 at 7:16
source share

If you still get 127.0.0.1 as an IP address, you need to add your own "proxy".

But be careful that you must change it before starting production!

Read this part: https://laravel.com/docs/5.7/requests#configuring-trusted-proxies

Now just add this:

 class TrustProxies extends Middleware { /** * The trusted proxies for this application. * * @var array */ protected $proxies = '*'; 

Now request () -> ip () gives you the correct ip

+3
Jan 29 '19 at 18:14
source share

in laravel 5.4 we cannot call ip static, this is the right way to get the user's ip

  use Illuminate\Http\Request; public function contactUS(Request $request) { echo $request->ip(); return view('page.contactUS'); } 
+2
Jun 30 '17 at 9:12
source share

If you want the client IP address and your server to be behind aws-elb, then enter the following code. Tested for laravel 5.3

 $elbSubnet = '172.31.0.0/16'; Request::setTrustedProxies([$elbSubnet]); $clientIp = $request->ip(); 
+2
Oct 24 '17 at 3:18
source share

There are 2 things to take care of.

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

 request()->ip(); 

2) Think about the configuration of your server, it can use a proxy or load balancer (especially in the AWS ELB configuration)

If this is your case, you need to configure trusted proxies, or perhaps even set the option Trusting All Proxies .

What for?

Because, as your server, you will receive your IP proxy / balancer.

How?

If you are not using AWS balance-loader

Go to App\Http\Middleware\TrustProxies

and make the $proxies declaration look like this:

 protected $proxies = '*'; 

Now check it out and celebrate because you just got rid of throttle middleware issues. It also relies on request()->ip() and without setting TrustProxies you can TrustProxies all your users log in instead of only blocking the culprit's IP address.

And since throttle middleware not described properly in the documentation, I recommend watching this video

Tested in Laravel 5.7

+2
Dec 18 '18 at 22:48
source share

If you call this function, you easily get the IP address of the client. I have already used this useful code in my existing project.

 public function getUserIpAddr(){ $ipaddress = ''; if (isset($_SERVER['HTTP_CLIENT_IP'])) $ipaddress = $_SERVER['HTTP_CLIENT_IP']; else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; else if(isset($_SERVER['HTTP_X_FORWARDED'])) $ipaddress = $_SERVER['HTTP_X_FORWARDED']; else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; else if(isset($_SERVER['HTTP_FORWARDED'])) $ipaddress = $_SERVER['HTTP_FORWARDED']; else if(isset($_SERVER['REMOTE_ADDR'])) $ipaddress = $_SERVER['REMOTE_ADDR']; else $ipaddress = 'UNKNOWN'; return $ipaddress; } 
+2
Jan 23 '19 at 10:31
source share

In the new version, you can get it with an assistant request

 request()->ip(); 
0
May 28 '18 at 4:51
source share

When we need the ip_address user:

 $_SERVER['REMOTE_ADDR'] 

and want the server address:

 $_SERVER['SERVER_ADDR'] 
-2
Aug 10 '16 at 5:58
source share



All Articles