Disagreement about the ip address of client

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

if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy

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?


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.


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!