How to get client IP address in Laravel 5+

前端 未结 18 2731
你的背包
你的背包 2020-11-27 11:11

I am trying to get the client\'s IP address in Laravel.

It is easy to get a client\'s IP in PHP by using $_SERVER[\"REMOTE_ADDR\"]. It is working fine

18条回答
  •  没有蜡笔的小新
    2020-11-27 12:06

    If you have multiple layer proxies just like CDN + Load Balancer.
    Using Laravel Request::ip() function will get right-most proxy IP but not client IP.
    You may try following solution.

    app/Http/Middleware/TrustProxies.php

    protected $proxies = ['0.0.0.0/0'];
    

    Reference: https://github.com/fideloper/TrustedProxy/issues/107#issuecomment-373065215

提交回复
热议问题