PHP: Get IP/Host and Proxy Information

April 12th, 2008 · 3 Comments

Below is a method I use to detect the IP Address and Host Name as well as Proxy Information of a visitors.

  1. if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"] != ""){
  2.      // for proxy
  3.      $IP = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
  4.      $proxy = $HTTP_SERVER_VARS["REMOTE_ADDR"];
  5.      $host = @gethostbyaddr($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]);
  6. }else{
  7.      // for normal user
  8.      $IP = $HTTP_SERVER_VARS["REMOTE_ADDR"];
  9.      $host = @gethostbyaddr($HTTP_SERVER_VARS["REMOTE_ADDR"]);
  10. }

Tags: PHP