tags:

views:

371

answers:

1

Duplicate:

How do I find a user's IP address with php?

Edit: Server is a Debian linux box running PHP5 through suPHP. Above post has been read. This code covers all points mentioned but still returns unknown addresses.

My code always requires that the remote IP address be known. It doesn't matter if we pick up the proxy address once we can get some IP address for the access.

Function below is what we current use however in over 20% of kits, the server falls through to the unknown case and has nothing in the $_SERVER var.

function getip()
{
    if ( $_SERVER["HTTP_CLIENT_IP"] && strcasecmp($_SERVER["HTTP_CLIENT_IP"], "unknown") )
    {
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    }
    else if ( $_SERVER["HTTP_X_FORWARDED_FOR"] && strcasecmp($_SERVER["HTTP_X_FORWARDED_FOR"], "unknown") )
    {
        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    }
    else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
    {
        $ip = getenv("REMOTE_ADDR");
    }
    else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
    {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    else
    {
        $ip = "unknown: ".var_dump($_SERVER, true);
    }
    return($ip);
}
A: 

Some random blog found via google . The interesting code is actually in the comment rather than what the blogger has mentioned.

function getIpAddress() 
{
    return (empty($_SERVER['HTTP_CLIENT_IP'])?(empty($_SERVER['HTTP_X_FORWARDED_FOR'])?
    $_SERVER['REMOTE_ADDR']:$_SERVER['HTTP_X_FORWARDED_FOR']):$_SERVER['HTTP_CLIENT_IP']);
}




function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
dassouki
Very similar to what I have except we check $_SERVER['REMOTE_ADDR'] and it is coming up blank too.
Ryaner