Forum Moderators: phranque

Message Too Old, No Replies

Reverse DNS Lookup with PHP

And whatever other tools are required

         

MatthewHSE

10:00 pm on Dec 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just read How To Verify Googlebot [googlewebmastercentral.blogspot.com], and immediately thought how useful this would be to prevent Google from getting session ID's in links. The problem is, I don't know how to do DNS lookups that way.

PHP is the only programming language I really know. So how can one use PHP (and probably some other tools/services) to do the reverse DNS and forward DNS lookups as suggested in the article referenced above?

Moderators, I posted this thread here because I'm guessing the technique involves several technologies, not just PHP. But please feel free to move this if it belongs in the PHP forum...

Receptional Andy

4:38 pm on Dec 8, 2006 (gmt 0)



Reverse DNS is easy with PHP - you get the visitor's IP address stored in the $_SERVER['REMOTE_ADDR'] variable and then convert it to a hostname with gethostbyaddr [php.net]

E.g.


$first_ip=$_SERVER['REMOTE_ADDR'];
$hostname=gethostbyaddr($first_ip);

You reverse the lookup with gethostbyname [php.net]


$second_ip=gethostbyname($hostname);
if ($second_ip = $first_ip) {
// IPs matched
}

Added: there will be a bit of overhead time in doing this (depending on your server) so you would want to minimise how many lookups you do (i.e. only check visitors with Googlebot's UA)

[edited by: Receptional_Andy at 4:40 pm (utc) on Dec. 8, 2006]