0

I made this code to get users ip on my site currently i am checking it in my localhost but it is returning something like ::1. I am using xampp package , my code is

<?php
    echo $_SERVER['REMOTE_ADDR'];
?>

I would have got my ip 127.0.0.1 as i am on a localhost but it is returning ::1.

3 Answers3

0

The IP address "::1" is the new Internet addressing system. If you are writing code to handle IP addresses it will come across these more and more.

Peter Blue
  • 192
  • 6
  • ::1 is an exact IP address but is the IPv6 equivalent of 127.0.0.1 – Peter Blue Aug 24 '15 at 07:12
  • I don't think you can. IPv4 has about 4.3 Billion addresses whereas IPv6 has about 340,282,366,920,938,463,463,374,607,431,768,211,456 – Peter Blue Aug 24 '15 at 13:39
  • If you are getting ::1 it seems like your network has been set-up to use IPv6. If you are running Linux or FreeBSD you can force it to use IPv4 along side IPv6. – Peter Blue Aug 24 '15 at 13:54
0

According to this answer on SO

Localhost always translates to the loopback IP address 127.0.0.1 in IPv4, or ::1 in IPv6

So getting ::1 is actually expected behavior.

If you want to convert ::1 to the IPv4 equivalent of 127.0.0.1, you can use PHP's inet_ntop() function. See this question for more information.

$packed = chr(127) . chr(0) . chr(0) . chr(1);
$expanded = inet_ntop($packed);

/* Outputs: 127.0.0.1 */
echo $expanded;

$packed = str_repeat(chr(0), 15) . chr(1);
$expanded = inet_ntop($packed);

/* Outputs: ::1 */
echo $expanded;
Community
  • 1
  • 1
Tim
  • 2,123
  • 4
  • 27
  • 44
0

You can use dig with shell_exec

$ip = trim(shell_exec("dig +short myip.opendns.com @resolver1.opendns.com"));
echo ("My public IP: ".$ip);
mdamia
  • 4,447
  • 1
  • 24
  • 23