简体   繁体   中英

Cannot access visitor's ip address in php

i am trying to store ip addresses of people who are visiting my web site. For that i use the below given code.

$serverIP=$_SERVER['REMOTE_ADDR'];

but instead of getting an IP like 112.200.xxx.xxx (when i visit), i got 192.9.200.195 ..

somebody please help me

thanks in advance

tismon

try this, maybe its a proxy(?)

if ($_SERVER['HTTP_X_FORWARDED_FOR'])
{
  $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
  $ip = $_SERVER['REMOTE_ADDR'];
} 
echo $ip;

looks like you're thinking 192.9.200.195 is a local ip-adress - but its not, local adresses you mean are starting with 192.168. . 192.9.200.195 looks ok to me, if it's not, please try to explain you problem more detailed.

try

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;
}
echo getRealIpAddr();

You can also try this:

<?php
$var = file_get_contents('http://www.whatismyip.com/automation/n09230945.asp');
print $var;
?>

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