1

i have a contact form on my website, and i'm trying to know what is the IP address of the users, i've tried this PHP code on my local testing XAMPP:

function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP']))
//check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
 elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
 //to check ip is pass from proxy
  {
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  }
  else
  {
    $ip=$_SERVER['REMOTE_ADDR'];
  }
  return $ip;

}
$ipaddress= getRealIpAddr();

echo "my ip address is" . $ipaddress;

but the results is always like

"my ip address is::1",

I don't know what's wrong with the code.

Tiago Sippert
  • 1,324
  • 7
  • 24
  • 33
john
  • 31
  • 4
  • 1
    possible duplicate of [What is the most accurate way to retrieve a user's correct IP address in PHP?](http://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php) – Aleks G Jun 26 '13 at 13:56
  • @AleksG I think it's more a duplicate of this [how to show The IP address in IPv4 which show ::1 in the web page](http://stackoverflow.com/questions/11195688/how-to-show-the-ip-address-in-ipv4-which-show-1-in-the-web-page?rq=1) , however what you posted should be marked as related since there is a lot of useful information in that SO question. – Tim Sanders Jun 26 '13 at 14:08

2 Answers2

8

That's because you're on your local machine - ::1 is one of the IPv6 addresses of localhost. It's similar to 127.0.0.1. That's because you're accessing your script locally. If you upload this script to a server, and access it via an internet connection, you will be able to see your external IP.

nickb
  • 59,313
  • 13
  • 108
  • 143
casraf
  • 21,085
  • 9
  • 56
  • 91
2

::1 is the same as 127.0.0.1, but in IPv6 notation, so maybe your code is right! Can you check it on a server and compare your results with something like "whatismyip.com" output?

user2520968
  • 358
  • 1
  • 3
  • 11