Disagreement about the ip address of client


#1

I’ve been using the following tried and true php code:

if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
{
  $myipaddr=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
{
  $myipaddr=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
  $myipaddr=$_SERVER['REMOTE_ADDR'];
}

to return the clients IP address, but OFTEN I find the apache access log has a different address than the one provided by the above code. Any comments on which one the access log uses or how it gets an IP address of the client?

Bill


#2

The HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR variables contain, respectively, the contents of the “Client-IP” and “X-Forwarded-For” headers sent to the web server by the client. These headers are typically inserted by web proxy servers. We do not currently employ any proxy servers in our infrastructure, so the only values you are likely to find in these headers are ones that are either useless (because they are generated by a proxy internal to a client’s network) or deliberately full of lies.

The correct client IP will always be in the REMOTE_ADDR variable.


#3

Andrew,

Thank you! This code is all over the web, and I’m using it to find people hacking my website, so OF COUSE they are trying to throw me off!