PHP Script for checking when Googlebot visits

Sometimes I want to know when the Googlebot has visited a page. The following script will:

  • Check the referrer for the Googlebot
  • Cross check the IP that it’s really Google IP
  • Email you with the details.



if(eregi("Googlebot",$_SERVER['HTTP_USER_AGENT'])){
$ip = $_SERVER['REMOTE_ADDR'];
$name = gethostbyaddr($ip);
$host = gethostbyname($name);
if(eregi("Googlebot",$name)){
if ($host == $ip){
$to = basename($_SERVER['PHP_SELF']) ."@" . str_replace("www.", "", $_SERVER['SERVER_NAME']); // OR UPDATE TO YOUR EMAIL
$subject = "Googlebot visited " . $_SERVER['SCRIPT_FILENAME'];
$message = $ip . "\n";
$message .= $name . "\n";
$message .= $_SERVER['HTTP_USER_AGENT'] . "\n";
$message .= $_SERVER['PHP_SELF'] . "\n";
$email_from = basename($_SERVER['PHP_SELF']) ."@" . str_replace("www.", "", $_SERVER['SERVER_NAME']);
$headers = "From: <$email_from>\n";
mail($to, $subject, $message, $headers);
}
}
}

Author: Online Sales

Owner of Mega Products Ltd. Everyday superhero web-bloke.