简体   繁体   中英

How to get real client ip or geolocation in php?

I have a user registration system on my site. I am using xml ip api of ip-api.com to detect the location of users. My problem is that the php script I am using returns proxy or wrong ip address of users ,and that is why I they can not set correct location in their profile.

I use the following conditional statement to get ip address :

   $ip=$_SERVER["REMOTE_ADDR"];
 if(empty($ip))
 {$ip=

$_SERVER["HTTP_X_FORWARDED_FOR"];}

It returns wrong ip address for some users.

Is there any other way to get real ip or geolocation in php?

You can see this topic : How to get Real IP from Visitor?

function getUserIP()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];
    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }
    return $ip;
}
$user_ip = getUserIP();

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