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:-


It gives me Server's IP Address (, 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 ( (i.e. visitor's IP) at [url][/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:


rehan arshad answered 2 years ago

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;
        $ip = $remote;

    return $ip;

$user_ip = getUserIP();

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


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