Finding remote host info

This is how I find out where a remote client is coming from. It takes into account proxies and tries to get the hostname using gethostbyaddr(). Frankly, this was written many years ago so I’m not sure if I’d do it the same way. If nothing else, it should be a good starting point for you.

// is this request coming from a proxy server?
if (empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
  $proxyip = $_SERVER['REMOTE_ADDR'];
  if (! empty($_SERVER['REMOTE_HOST']))
  {
     $proxyname = $REMOTE_HOST;
  } else {
      $getname = @gethostbyaddr($_SERVER['REMOTE_ADDR']);
      if (strcmp($getname,$_SERVER['REMOTE_ADDR'])!=0) 
         $proxyname = $getname;
  }
  $hostip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  $getname = @gethostbyaddr($_SERVER['HTTP_X_FORWARDED_FOR']);
  if (strcmp($getname,$_SERVER['HTTP_X_FORWARDED_FOR'])!=0) 
     $hostname = $getname;
}
elseif (! empty($_SERVER['REMOTE_ADDR'])) // no, it's not
{
   $hostip = $_SERVER['REMOTE_ADDR'];
   $getname = @gethostbyaddr($_SERVER['REMOTE_ADDR']);
   if (stcmp($getname,$_SERVER['REMOTE_ADDR'])!=0) 
      $hostname = $getname;
}


After all that, you’d have a $proxyip and (probably) a $proxyname if the visitor was coming to your server through a proxy. In case you didn’t know, that includes people who use a device like a wireless router at home. Proxy or no, you may also have a $hostip and $hostname for the computer that’s browsing your site.

Good luck.