Log Googlebot visits

<?php
/*
-- SQL table structure for table `googlebot_visits`.
-- Import to your database, e.g. using phpMyAdmin.
CREATE TABLE `googlebot_visits` (
  `id` int(11) NOT NULL default '0',
  `datetime` datetime NOT NULL default '0000-00-00 00:00:00',
  `uri` varchar(255) NOT NULL default '',
  `agent` varchar(255) NOT NULL default '',
  `ip` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;
*/

# Catch Googlebot when indexing.
if (@preg_match('/Googlebot/', $_SERVER['HTTP_USER_AGENT'])) {
    # Save the entry to database (adapt to your preferred way).
    # Note that only one row exists in the table which will be
    # overridden on updates.
    $db->query('
        REPLACE INTO `googlebot_visits`
        (id, datetime, uri, agent, ip)
        VALUES (1, NOW(), ?, ?, ?);
    ', array(
        $_SERVER['REQUEST_URI'],
        $_SERVER['HTTP_USER_AGENT'],
        $_SERVER['REMOTE_ADDR']
    ));
}

# Retrieve the last Googlebot visit from database
# (again, adapt to your preferred way).
$row = $db->getRow('
    SELECT datetime, uri
    FROM `googlebot_visits`
    WHERE (id = 1);
');
# For date formatting, use date(), strftime() and strptime().
printf("Googlebot was last here at %s visiting '%s'.\n",
    $row['datetime'], $row['uri']);
?>