john
john

Reputation: 31

detect user ip address using PHP

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.

Upvotes: 1

Views: 2093

Answers (2)

casraf
casraf

Reputation: 21694

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.

Upvotes: 8

user2520968
user2520968

Reputation: 368

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

Upvotes: 2

Related Questions