If you want to log what a user is doing, or have any other reason to get his ip using HTTP_CLIENT_IP to get the remote user IP is not at all enough, if the client is connecting through a proxy (for an example your site is configured behind an apache_mod_proxy) you should also test for HTTP_X_FORWARDED_FOR and fall back on REMOTE_ADDR or you might always be getting the proxie's IP.

PHP:
  1. function getIP() {
  2. $ip;
  3.  
  4. if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
  5. else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");
  6. else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
  7. else $ip = "UNKNOWN";
  8.  
  9. return $ip;
  10.  
  11. }

Leave a Reply

Creative Commons License
This work is licensed under a Creative Commons Attribution 2.0 License.