How do I find my real IP Address in PHP (Getting Server’s IP rather than visitor’s IP

famaash asked 2 years ago

How do I find my real IP Address?

If I do:-

[HTML] $_SERVER['REMOTE_ADDR'][/HTML]

It gives me Server's IP Address (157.191.122.36), and it gives me same values even when my site is accessed through different locations and countries.

Your site somehow, gives me the right IP (64.74.66.230) (i.e. visitor's IP) at [url]http://www.whatismyip.com/[/url], but I can't get the same value through the above PHP code.

Any help.

I tried different ways but None of the solutions worked for me. It is clear now though that by using [HTML]$_SERVER["REMOTE_ADDR"][/HTML], I get the server's IP address rather than the visitor's.

Many Thanks.

2 Answers
Rob Vargas Staff answered 2 years ago

I'm not a very good programmer, but this looks like a good solution:

[url]http://stackoverflow.com/questions/15699101/get-client-ip-address-using-php[/url]

rehan arshad answered 2 years ago
<?PHP

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();

echo $user_ip; // Output IP address [Ex: 177.87.193.134]


?>

Know the answer? Login or sign up for an account to answer this question.
Sign Up