2

Duplicate:

How to get the client IP address in 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);
}
vbnm
  • 83
  • 6
Ryaner
  • 751
  • 6
  • 16
  • 1
    What Server are you using? You may want to read http://stackoverflow.com/questions/55768/how-do-i-find-a-users-ip-address-with-php –  May 11 '09 at 13:30
  • Read that. If you look at the above code, it checks all the fields mentioned in that post. – Ryaner May 11 '09 at 13:40
  • What Server are you using? versions would be very useful to know –  May 11 '09 at 13:43
  • 1
    You can't trust the HEEP_X_FORWARDED_FOR header, as it's set by the client. – Powerlord May 11 '09 at 13:50
  • Had forgotten about this. It _wasn't_ a duplicate. Code was actually not getting a remoteip set as I said. Issue was eventually tied back to the script being called by an automated script. PHP doesn't set the IP variable not called through a webserver. – Ryaner Feb 07 '12 at 12:44
  • See this answer: http://stackoverflow.com/a/6718472/996926 – advncd Mar 11 '16 at 00:32

1 Answers1

0

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
  • 6,286
  • 7
  • 51
  • 81
  • Very similar to what I have except we check $_SERVER['REMOTE_ADDR'] and it is coming up blank too. – Ryaner May 11 '09 at 13:41