In this short tutorial we will cover how to send yourself an email via a PHP Script when Google’s spider has visited your site. Step 1. When googlebot visits your site we can easily tell it’s google and not a normal user by its User Agent. To call up the useragent in php we use a variable called $_SERVER[‘HTTP_USER_AGENT’]. Step 2. The Googlebot user agent is always “Googlebot”, so all we need to do is check whether or not the useragent is Googlebot. To do this we will use a php function called strpos, this function returns the position of the string, if the string isn’t found it will return false. Step 3. Firstly we need to write an IF statement to check if the useragent is set to googlebot.
if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Googlebot' ) !== false )
{
    // Useragent is Googlebot
}

The above code checks that useragent doesn’t return a false value. Step 4. So we have a piece of code that checks the useragent, we now want it to email us when this happens, for this we will use a php function called mail.
if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Googlebot' ) !== false )
{
    /* The email address we want
    to send the email to */
    $email_address = 'example@domain.com';


    // Send the email
    mail($email_address,'Googlebot Visit', 'Googlebot has visited your page: '.$_SERVER['REQUEST_URI']);
}

Leave a Reply

Your email address will not be published. Required fields are marked *