How to get client IP address in Laravel 5+

前端 未结 18 2704
你的背包
你的背包 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条回答
  •  Happy的楠姐
    2020-11-27 12:14

    If you are still getting 127.0.0.1 as the IP, you need to add your "proxy", but be aware that you have to change it before going into production!

    Read "Configuring Trusted Proxies".

    And add this:

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

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

提交回复
热议问题