Writing a PHP Page for the googlebot / google’s cache

line Tags: , ,

Here’s something cool to share.

My calcylator project is written in php, it’s one of those “on-line tools” where users can create an account, log in, and can calculate personal profit & loss sheets for ebay.

Like most websites there are “log in” sections, now these pages need to be dealt with differently for search engines & users. Search engines will not be able to log in, but you may still want to deliver some content to them. Take my example, calcylator is running google adsense, and for adsense to work the googlebot (search engine) needs to be able to “see” some content, thinking this through, every time googlebot visited the log in or other protected pages all it would see is “access denied” error pages.

To target google with some “special” content, google kindly present themselves as google in their agent string. Now php even provide a simple tutorial on distinguishing user agent, so this is all I needed to do:

<php

function find_googlebot() {

# This is where we look for the googlebot
$user_agent = $_SERVER["HTTP_USER_AGENT"];

if (preg_match (”/[Gg][Oo][Oo][Gg][Ll][Ee]/”, $user_agent )) {
return true;
}

#For debugging, uncomment this and the agent will appear in the source of the html.
#echo “<!– $user_agent –>”;

}

if (find_googlebot()) {
include(”google-content.php”);
exit;
}

?>

If I quickly run though what I’ve done. We have a function called find_googlebot , we run a test (look for the word google), if we get a match, then we return “true” and run the php google-content.

Here is a working example, if you visit http://www.calcylator.com/index.php?cmd=myhome you’ll see a login page (or your account if you log in) and google will see:

http://www.google.com/search?q=cache:25ez2GhuhDQJ:www.calcylator.com/index.php%3Fcmd%3Dmyhome+site:http://www.calcylator.com

Have fun, just remember, google cache doesn’t update that often, so if you present a mistake, you may be stuck with it for a while ;)

nick

 

Got something to say?

 

Some other things that might interest you...

---