The easiest way is probably to check the USER AGENT string supplied by the visitor. This value is supplied by the client so it won't work if the visitor fails to supply this information (like if the visitor is using curl or is a hacker or something) but most visitors (and bots) are well behaved.
Here's a super simple script from a very long time ago but it's hardly comprehensive as far as a bot list goes:
<?
$user_agent = $_SERVER['HTTP_USER_AGENT'];
function is_bot($user_agent) {
$spiders = array('Googlebot', 'MSNBOT', 'FAST-WebCrawler', 'Gigabot', 'YahooSeeker', 'ZyBorg');
foreach($spiders as $key=>$bot) {
echo 'testing:' . $bot . '<br>';
if (stristr($user_agent, $bot)) {
return TRUE;
}
}
// if we reach this point, we've tried all the bots with no match
return FALSE;
}
echo 'result:' . is_bot($user_agent);
?>
I bet you could find a better one by googling around.