Get an email when Google visits

When googlebot comes (if it does!) to pages that contains this code, you get an e-mail sent to the address you specify.

Insert the PHP code below in all pages where you want to track whether google is visiting the page or not.

<?php
//Change the following to your email address
$email = “myemail@something.com”;
if(eregi(”googlebot”,$_SERVER['HTTP_USER_AGENT']))
{
mail($email, “Googlebot detected”,
“Google has crawled : “.$_SERVER['REQUEST_URI']);
}
?>

If you have include a file like header.php, footer.php,.. etc. everywhere then you can simply place this code in any of those files to track all those pages. MT users can place it in their template and all their pages will be tracked.

PERL users can use:

# give path of sendmail
$mailprog = ‘/usr/sbin/sendmail’;
#Change the following to your email address
$email = myemail@something.com;
$browser = $ENV{’HTTP_USER_AGENT’};
if ($browser =~ m/googlebot/i) {
open (MAIL, “|$mailprog -t”) || die “ERROR: Problem Opening - $mailprog!\n”;
print MAIL “From: $email\n”;
print MAIL “To: $email\n”;
print MAIL “Subject: Googlebot detected\n\n”;
print MAIL “Google has crawled : “.$ENV{’REQUEST_URI’};
close (MAIL);
}

33 Responses to “Get an email when Google visits”

  1. Ks Weblog Says:
    Get Email if Googlebot Comes
    From scriptygoddess When googlebot comes (if it does!) to pages that contains this code, you get an e-mail sent to the address you specify….

  2. Wandering Around Says:
    Get Email if Googlebot Comes
    From scriptygoddess When googlebot comes (if it does!) to pages that contains this code, you get an e-mail sent to the address you specify.

  3. Fil Says:

    I get a parse error everytime I call this piece of code on my site.
    It seems that the line “$email ” is failing.

    Any ideas?

  4. Jayant Kumar Gandhi Says:

    Thanks to the editors for inserting that bug :D
    Change
    $email = myemail@something.com;

    to

    $email = “myemail@something.com”;

  5. Fil Says:

    Had I been paying attention this morning, I would’ve noticed that!
    Thanks for the quick response.

  6. Jayant Kumar Gandhi Says:

    Go get some caffiene :D

  7. Jennifer Says:

    “Thanks to the editors for inserting that bug :D”

    Actually I posted exactly what you submitted (I’ll forward it to you).

    Post is now updated with your correction.

  8. Bloggins Says:
    Google Schiester
    Reading the scriptygoddess I saw a post authored by Jayant Kumar Gandhi. In this post, Jayant states how using simple php, you can make your server email you whenever the famed "GoogleBot" visits your page. This was particularly interesting t…

  9. Jayant Kumar Gandhi Says:

    ah ya. probably if was just sleep taking over me when i posted it late @ night.

  10. lotas Says:

    hmmmm. only one thing. what happens when google comes in and visits about 800+ pages, or in my case 35000+ pages! you go to check your mail and BANG! mailserver explodes! :P

  11. Jayant Kumar Gandhi Says:

    Just do it for you main page or important ones or new ones.
    wot say :)

  12. Savage Nomads Says:
    Monitoring your site
    This is a pretty cool script that will email you when the googlebot indexes your pages. I think I may try to add this script this weekend, I’ll let you know how it goes. Nope I don’t have a life….

  13. endoid [at] endoid [dot] net Says:

    Well done scripties, this is really useful.
    There is one thing that the MovableType users will need to remember though: it’s so trivial that we will easily forget it.

    To add the PHP code to the MT templates you need to make sure that MT generates it’s files (or at least the main index) as PHP files and not simply as HTML. That way the PHP code is executed instead of being returned as text.

    In the MT administration area, click on “Templates > Main Index ” and change the “Output File” to “index.php” if it’s “index,html”. If you call it “main.html” or indeed anything, just make sure that the file’s extension is “.php” and test it out just to make sure.

    For those of you using Linux and you’ve access to the “wget” program you can have it act as the Googlebot engine by running the following:

    wget -U googlebot http://www.put-your-site-url-here.com

    That will let you know that the test is working.

    ps: my *might* need to remove the old “index.html” file (I recommend you rename it first) in case your web server looks for “.html” files before it looks for “.php”. And remember, you *do not) need to change your archived files: they can remain as “.html” if you have many of them.

  14. [ entrivia ] Says:
    Get notified when Google Crawls
    The scriptygoddesses have a very simple and very effective bit of PHP code to let you know that Google has crawled your site. It depends on your webserver running PHP and that you’ve access to sendmail (but you *may* be…

  15. thomas Says:

    does it work on .html file? What if the webhost doesn’t support .php or PERL ? can I still get notified when Googlebot come?

  16. endoid [at] endoid [dot] net Says:

    To Thomas:
    Normally no, this code (in either PHP or Perl) will not work in a .html file. Webservers know to execute code that lives inside .php files *before* they are sent our to your web browser and this is how thye web server is able to send you the email to tell you that Google has visited. This is known as “server-side” code becasuse it’s code that runs on the server. If your web server is not set up to run code and only serve up .html code then this won’t work for you.
    The other code that works on the web is JavaScript or VBScript. With this case though it runs in your web brower, but that’s code running on your browser machine and not on the server. Your browser machine doesn’t have any way of knowing what other borwsers have come to the website. Other browsers in this case includes the GoogleBot so there’s no easy way that client side JavaScript or VBScript will work.

    [I say no easy way, but it would be possible if your webserver was set up to be frightfully insecure and there's no way I'm going to get into how to do that. You'll be so much better off finding a server with php or a hostings sote that allows php instead.]
    /e

  17. Jayant Kumar Gandhi Says:

    It maybe be possible using SSI if your host allows it. You can exec the sendmail program with the parameters of the email.
    I am not sure how will one compare that the useragent is google or not.
    perhaps you can make a shell script or a executable program and pass all the related parameters and that shell script/program will invoke sendmail.

    Presently I have no plans of implementing it myself. If a lotta people request it, I might think of it.

  18. Fil Says:

    Would there be anyway to have the email also send the user’s agent and IP address?

    Im sort of using this as a notification of when google visits so I know how often they’re crawling my site. I for one, don’t want my site crawled and would like to have the IP’s of the google crawlers so that I can ban them.

    Any other suggestions to aid me in this task?

  19. Daisyhead Says:
    Scriptygoddess goodness
    I haven’t perused Scriptygoddess lately so I’m missing out on all kinds of little gems like this handy little piece of code which notifies you by email when a Googlebot visits your site and this code which allows you to…

  20. Jayant Kumar Gandhi Says:

    In PHP $_SERVER['HTTP_USER_AGENT'] will have the user-agent while $_SERVER['REMOTE_ADDR'] has the IP address.

    The better option is:-
    See http://www.google.com/bot.html

  21. Fil Says:

    Its too bad that the google bot doesnt seem to obey those tips.

    I use both a robots.txt and the META tag for robots to no avail.
    GoogleBot just doesnt seem to care!

  22. pMachine HowTo Says:
    HowTo: Tell if the GoogleBot Has Visited
    Originally found at Scripty Goddess, here’s a code snippet you can add to your pages to tell you when the GoogleBot has found your site.

    <?php
    //Change the following to your email address
    $email = “myemail@something.com”;

    if(eregi(”googleb…

  23. Untitledlog Says:
    Sabe cuando Google revisa tu pagina
    Visitando scriptygoddess me encontre este script que te brinda la posiblidad de…

  24. Glimpse of a Grrl Says:
    Playing with Google
    Adi’s trying to see if Google crawls his site often. So we both installed this code snippet from :SG:. Since Google keeps hitting my site, I’m going to see if I can redirect them to him by the force of…

  25. mike Says:

    Well 1st off i been reading over this php stuff and looking at the codeing and i see stuff i get and all but well i am lost i have a web server called savant and i use it to host my web site and i been trying to do some stuff like Fil has posted and i cant get it to work if you could tell me how to get it to work i would o you one i have a cgi-bin well i can add what i like to it what i would like to do is when someone comes on my site i would like to send them a email thanking to for comeing to my site and they no that this email is comeing to them so it is not spam if you could tell me how i could go about doing this would be grate i been doing html have not got in to all this other stuff but I AM NOW thanks you can email me at itsmcfly32@ma.rr.com

  26. Nibble Guru Says:
    Get an email when Google comes visiting
    One of my friend wanted me to do this for him. I thought I will share this with you too. See the following PHP code below to achieve it. <?php if(eregi(”googlebot”,$_SERVER['HTTP_USER_AGENT'])) { mail(”myemail@something.com”, “Googlebot detected”, “…

  27. Rizwan Kassim's Public Log Says:
    Look Mom!
    See what I’ve done? The side bars (just left for now) are collapsable, and not in the stupid way that the blogrolling is collapsable… It actually doesn’t transmit the data if it doesn’t need to, meaning an even quicker download…

  28. Jenni Says:

    Where would you put the following pieces of info in the script?

    $_SERVER['HTTP_USER_AGENT']
    $_SERVER['REMOTE_ADDR']

    Thanks!

  29. Daisyhead Says:
    RSS feed updates
    I’ve updated my RSS feeds. You can now choose from posts only or posts with comments. Although I’m not sure the posts with comments feed is working. I used Jennifer’s template and I just copied and pasted it, but still…

  30. Jennifer Says:

    You mean to have those pieces of information mailed to you too?

  31. Jenni Says:

    Yes, but I figured out a better way. I just denied from googlebot.com in my .htaccess file. But thanks to the script, I knew they were crawling my site, so I could block them.

    Thank you anyway!!

  32. Anh-Kiet Ngo Says:

    I would like to comment for Perl users.

    $email = myemail@something.com;

    After that is changed into:
    $email = “myemail@something.com”;

    Be sure to add a “\” in front of the @ sign. Otherwise, it will not work. The end result would be:

    $email = “myemail\@something.com”;

    Computer Central

  33. Glimpse of a Grrl Says:
    Playing with Google
    Adi’s trying to see if Google crawls his site often. So we both installed this code snippet from :SG:. Since Google keeps hitting my site, I’m going to see if I can redirect them to him by the force of…