All pastes #1769634 Raw Edit

Detect robot and google visitors

public php v1 · immutable
#1769634 ·published 2010-01-28 18:34 UTC
rendered paste body
/** check if the user comes from google **/ function fromasearchengine(){  $ref = $_SERVER['HTTP_REFERER'];  $SE = array('/search?', 'images.google.', 'web.info.com', 'search.', 'del.icio.us/search', 'soso.com', '/search/', '.yahoo.');  foreach ($SE as $source) {    if (strpos($ref,$source)!==false) return true;  }  return false;}/** spider robot detector return spider name, or else return false **/function crawlerDetect($USER_AGENT) { $crawlers = array( array('Google', 'Google'), array('msnbot', 'MSN'), array('Rambler', 'Rambler'), array('Yahoo', 'Yahoo'), array('AbachoBOT', 'AbachoBOT'), array('accoona', 'Accoona'), array('AcoiRobot', 'AcoiRobot'), array('ASPSeek', 'ASPSeek'), array('CrocCrawler', 'CrocCrawler'), array('Dumbot', 'Dumbot'), array('FAST-WebCrawler', 'FAST-WebCrawler'), array('GeonaBot', 'GeonaBot'), array('Gigabot', 'Gigabot'), array('Lycos', 'Lycos spider'), array('MSRBOT', 'MSRBOT'), array('Scooter', 'Altavista robot'), array('AltaVista', 'Altavista robot'), array('IDBot', 'ID-Search Bot'), array('eStyle', 'eStyle Bot'), array('Scrubby', 'Scrubby robot') ); foreach ($crawlers as $c) { if (stristr($USER_AGENT, $c[0])) { return($c[1]); } } return false;}$whatCrawler = crawlerDetect($_SERVER['HTTP_USER_AGENT']);if ($whatCrawler) { echo "Robot:".$whatCrawler;}else{  echo "You're no a robot!";}