$_SERVER['REMOTE_ADDR'] is not returning ip address

∥☆過路亽.° 提交于 2019-12-24 10:53:03

问题


I am using $_SERVER['REMOTE_ADDR'] in php to find client's ip address.

$ipaddress=$_SERVER['REMOTE_ADDR'];

echo $ipaddress;

which return ::1

I also tried the following code,but that gives me same result as well.

if ($_SERVER['HTTP_CLIENT_IP'])
    $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if($_SERVER['HTTP_X_FORWARDED_FOR'])
    $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if($_SERVER['HTTP_X_FORWARDED'])
    $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if($_SERVER['HTTP_FORWARDED_FOR'])
    $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if($_SERVER['HTTP_FORWARDED'])
    $ipaddress = $_SERVER['HTTP_FORWARDED'];
else if($_SERVER['REMOTE_ADDR'])
    $ipaddress = $_SERVER['REMOTE_ADDR'];
else
    $ipaddress = 'UNKNOWN';

What am I doing wrong?How can I get clients ip?

I would use the ip to find client's location via ipinfo.io.

Thanks for your time.


回答1:


::1 is the actual IP. It is an ipv6 address (i.e. localhost). If you were using ipv4 it would be 127.0.0.1.

If you want to get a different IP address, then you'll need to connect to the server through a different network interface.



来源:https://stackoverflow.com/questions/29618903/serverremote-addr-is-not-returning-ip-address

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