Whenever I start up a new website, I am always keen on knowing when Googlebot if going to come and visit for the first time and after that first visit when it returns for the second and third time. Now Google is only going to find and index your website if there is ATLEAST one other website that is linking to you.
But im not going to talk about the whole SEO thing, instead i'm am going to quickly go over how I made a PHP script which tells me when Googlebot comes to my website.
Googlebot always identifies it's self by the useragent header, and PHP has a $_SERVER variable which contains that, so really it's just going to be a matter of checking the server variable with what's in the useragent header. We can get information on Googlebot and what the useragent it uses is, by doing a quick search in Google for "Googlebot useragent".
Now we have the useragent Google uses (which is simply Googlebot) we just need to make a script which, when triggered checks the useragent and if it's a match for Google sends us a nice email. First we create a new text doc and rename it to googlecheck.php, open this with notepad or your favorite text editor.
The first line we want to type in is Next we'll declare the email address we want the notification sent to, the subject of the email, and the body.
$address = "email@domain.com"; $subject = "Google has visited"; $body="Google has crawled!
Date:".date(?d-m-Y?)." to my site at ".date("g:i A");
Now that is good, but if we have more then one website it will be hard to track which site it came from, so lets modify our subject.
$subject = "Google has visited ".$_SERVER[?HTTP_HOST?];
Thats better, now the site domain will be added to our subject.
Now we need to get the useragent and check that it's Google. $useragent = $_SERVER[?HTTP_USER_AGENT?]; $google = "Googlebot"; $isgoogle = strpos($useragent, $google);
If ($isgoogle === false) { //not a Google hit } else { //This is Google
So lets go through the above lines, we set the variable $useragent to the useragent in the headers, and then set the $google variable
to the Google useragent ? then we check to see if Google is in the headers by using to strops function and lastly put it into a IF statement, So now we need to send the email.
mail($address,$subject,$body); and close the IF statement }
then ?> to close the PHP file.
Now this works fine, however there is two things that we should really do. First it would be nice if we could tell which page that Googlebot visted, and second we should put some headers on our email.
Lets modify the body so we know which page Google crawled. $body="Google has visited!
Date:".date(?d-m-Y?)." to our site at ".date("g:i A")." Crawled the page ".$_SERVER[?PHP_SELF?];
and lets put some headers onto our email $header = "From: ".$address."" . " " . "Reply-To: ".$address."" . " " . "X-Mailer: PHP/" . phpversion(); mail($address,$subject,$body,$header);
That looks perfect! We what the script to trigger when the page gets loaded, so all we need to do is open the pages we want this file to be trigged from (like our index or page header) and at the top of the page do
include(?googlecheck.php?); ?>
Now all we have to do is wait for Googlebot to come along :-)
Nicholas Fauchelle has sinced written about articles on various topics from Software, web development. What to see just the finished script, simply go here http://www.nicksblogspot.com/detect-when-googlebot-has-come-to-visit. Nicholas Fauchelle's top article generates over 880 views. Bookmark Nicholas Fauchelle to your Favourites.