问题
I'm unable to get the IP address of the client which I need to determine his current location.
I've used request->ip(), $_SERVER['REMOTE_ADDR'] and I always get a 127.0.0.1 result which is not what I want.
What am I doing wrong?
回答1:
request->ip()
will give you client IP. You're getting 127.0.0.1
in because you're trying to access your local project from the same machine.
回答2:
Sometimes your clients use your application through a proxy, so you should not depend on $_SERVER['REMOTE_ADDR'].
Check out this link (with a little concern on securities):
How to get the client IP address in PHP?
回答3:
I found a way how to fix it. But beware that you have to change it before going production!!
Read this part: https://laravel.com/docs/5.7/requests#configuring-trusted-proxies
And 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
回答4:
There is client IP address this code($_SERVER['REMOTE_ADDR']
) applied online project then it will work successfully . it will try..
来源:https://stackoverflow.com/questions/41286310/laravel-get-client-ip-address-getting-always-127-0-0-1-result