Laravel - Get client IP address - Getting always 127.0.0.1 result

不想你离开。 提交于 2019-12-12 19:21:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!