0

I use the following code to collect IP addresses, but sometimes it happens that the output $ip is 127.255.255.255. Can someone please explain me why this occurs?

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}
$ip = ip2long($ip);
$ip = intval($ip);
  • Perhaps you should stop using random HTTP headers as sources of information and only exclusively use the only reliable source, which is `$_SERVER['REMOTE_ADDR']`...!? – deceze Jun 17 '16 at 09:59
  • 1
    Have a look [here](https://stackoverflow.com/questions/527638/getting-the-client-ip-address-remote-addr-http-x-forwarded-for-what-else-coul) for detailed info on getting the client IP address in php. [This](https://stackoverflow.com/questions/7445592/what-is-the-difference-between-http-client-ip-and-http-x-forwarded-for) could also be of interest. – cb0 Jun 17 '16 at 10:02
  • Thanks guys. I've read everything you shared, but I'd like to know why the loopback IP address shows up. – user6169665 Jun 17 '16 at 15:22

0 Answers0