Duplicate:
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);
}