Support the ongoing development of Laravel.io →
Requests
Last updated 2 weeks ago.
0

First of all you have to figure out a way to determine the "client".

Is it a logged in user or just a visitor?

Last updated 2 weeks ago.
0

Usually you should just be able to look in the $_SERVER variable..

Without knowing details about your hosting setup it's hard to give you a proper answer. It sounds like you might be behind a proxy or some kind of load balancer? You can try doing:

dd($_SERVER)

in a test route/view, and see what all the variables are coming back as. Figure out which one has your IP when you're looking at this on your host and that's the variable you need to use.

Last updated 2 weeks ago.
0

Request::getClientIp() should work, but you might need to setup trusted proxies, if you are behind some kind of proxy and the IP is passed as x-forwarded-for header. See http://symfony.com/doc/current/components/http_foundation/trus...

Last updated 2 weeks ago.
0

Did you try some

Request::getClientIp(true)


public string getClientIp(Boolean $proxy = false)

Returns the client IP address. This method can read the client IP address from the "X-Forwarded-For" header when trusted proxies were set via "setTrustedProxies()". The "X-Forwarded-For" header value is a comma+space separated list of IP addresses, the left-most being the original client, and each successive proxy that passed the request adding the IP address where it received the request from. If your reverse proxy uses a different header name than "X-Forwarded-For", ("Client-Ip" for instance), configure it via "setTrustedHeaderName()" with the "client-ip" key.

ref: http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/Re...

Last updated 2 weeks ago.
0

The proxy parameter was deprecated and is removed, it doesn't exist in 2.5 anymore.

Last updated 2 weeks ago.
0

I personally use this code block,

$request = Request::instance();
$request->setTrustedProxies(array('127.0.0.1')); // only trust proxy headers coming from the IP addresses on the array (change this to suit your needs)
$ip = $request->getClientIp();

Hope this will help.

Last updated 2 weeks ago.
0

Sign in to participate in this thread!

LaraJobs

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.