无法使用PHP为用户获取正确的IP


Cannot get correct IP for user using PHP

我正在尝试从IP地址获取国家/地区。问题是某些计算机上没有显示用户IP。一些测试它的用户得到了IP,但其他用户没有(在同一个WiFi网络上)。

这是我的密码。怎么了?

function VisitorIP()
{
    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;
}
$ip_address = VisitorIP();
$geodata = file_get_contents('http://api.ipinfodb.com/v3/ip-country/?key=0000&ip=' . $ip_address . '&format=json');
$decoded = json_decode($geodata);
$country = strtolower($decoded->countryCode);

怎么了?

一切。

服务器唯一可用的IP是存储在$_server['REMOTE_ADDR']变量中的IP
所以,你必须使用这个,而不是各种来源不明的HTTP头。