简体   繁体   中英

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

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 is the actual IP. It is an ipv6 address (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM