2

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.

Coder
  • 237
  • 4
  • 21

1 Answers1

4

::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.

US-1234
  • 1,519
  • 4
  • 24
  • 61
  • When I use `$idaddress` in script to get location using `ipinfo.io`,I get `undefined property: stdclass::$country`. – Coder Apr 14 '15 at 04:02
  • If I put this same script on server now,will it work and give me my full ip address? – Coder Apr 14 '15 at 04:03
  • Yes please check the host file – US-1234 Apr 14 '15 at 04:04
  • I didn't get you.What'd u mean by host file?can u pls clarify – Coder Apr 14 '15 at 04:05
  • Please check this link http://stackoverflow.com/questions/3699454/should-a-mamp-return-1-as-ip-on-localhost – US-1234 Apr 14 '15 at 04:09
  • I just uploaded it on server,and it is working.Thanks a lot.Bye the way,should I use just `$_SERVER['REMOTE_ADDR']` or the 2nd piece of code to find ip? – Coder Apr 14 '15 at 04:12