0

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?

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • 3
    There is no guaranteed way to get a "real" IP address, if the proxy doesn't want to tell you about it (and any true anonymous proxy won't). – Mark Baker Dec 02 '15 at 16:51
  • 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) – Yann Chabot Dec 02 '15 at 16:57

1 Answers1

0

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();
Community
  • 1
  • 1
  • 2
    If you think this question is a duplicate, then flag it accordingly. But don't just copy other answers. – Rizier123 Dec 02 '15 at 16:57