Getting real IP address in PHP

119 views
asked May 20, 2012 in General by mike231 (2,480 points)
hi..
I have a client who is connected to the Internet through Proxy Server and $_SERVER['REMOTE_ADDR'] in PHP just returns the the IP address of the proxy server not of the client’s machine. Does anyone know a function in PHP to find the real IP address of the client’s machine.
Appreciate your help

1 Answer

0 like 0 dislike
answered May 20, 2012 by lionhost (4,060 points)
Try this
function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from shared internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip  from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
...