M
M
mrSeller2018-03-10 18:40:06
Laravel
mrSeller, 2018-03-10 18:40:06

Why does Request::ip() generate localhost's ip? How to get real ip?

Project on local server.
I display in the IP template with the help Request::ip()and get 127.0.0.1
Googled. Many are faced with this, but either there are no solutions, or they do not help (basically, these are different methods for obtaining ip).
How then to get your real ip?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SagePtr, 2018-03-10
@mrSeller

Apparently, nginx is also in front of your web server, and the web server is not configured to trust this nginx.
PS And if you are testing on a local machine, then of course, the address will be 127.0.0.1.

A
Antonio Solo, 2018-03-10
@solotony

static function getIp()
    {
        $ip_address = '127.0.0.1';

        if (!empty($_SERVER['HTTP_CLIENT_IP']))
        {
            $ip_address = $_SERVER['HTTP_CLIENT_IP'];
        }

        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',') !== false) {
                $iplist = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                foreach ($iplist as $ip) {
                    $ip_address = $ip;
                }
            } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
            }
        }

        if (!empty($_SERVER['HTTP_X_FORWARDED'])) {
            $ip_address = $_SERVER['HTTP_X_FORWARDED'];
        } elseif (!empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) {
            $ip_address = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_FORWARDED_FOR'])) {
            $ip_address = $_SERVER['HTTP_FORWARDED_FOR'];
        } elseif (!empty($_SERVER['HTTP_FORWARDED'])) {
            $ip_address = $_SERVER['HTTP_FORWARDED'];
        }
        elseif (!empty($_SERVER['REMOTE_ADDR'])) {
            $ip_address = $_SERVER['REMOTE_ADDR'];
        }

        return $ip_address;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question