Skip to content

Instantly share code, notes, and snippets.

@ScottPhillips
Created June 10, 2012 08:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ScottPhillips/2904459 to your computer and use it in GitHub Desktop.
Save ScottPhillips/2904459 to your computer and use it in GitHub Desktop.
Detects a few common Search Bots
<?php
/* Detects some common web bots */
function detectBot($USER_AGENT) {
$crawlers_agents = strtolower('Bloglines subscriber|Dumbot|Sosoimagespider|QihooBot|FAST-WebCrawler|Superdownloads Spiderman|LinkWalker|msnbot|ASPSeek|WebAlta Crawler|Lycos|FeedFetcher-Google|Yahoo|YoudaoBot|AdsBot-Google|Googlebot|Scooter|Gigabot|Charlotte|eStyle|AcioRobot|GeonaBot|msnbot-media|Baidu|CocoCrawler|Google|Charlotte t|Yahoo! Slurp China|Sogou web spider|YodaoBot|MSRBOT|AbachoBOT|Sogou head spider|AltaVista|IDBot|Sosospider|Yahoo! Slurp|Java VM|DotBot|LiteFinder|Yeti|Rambler|Scrubby|Baiduspider|accoona');
$crawlers = explode("|", $crawlers_agents);
if(is_array($crawlers) {
foreach($crawlers as $crawler) {
if (strpos(strtolower($USER_AGENT), trim($crawler)) !== false) {
return true;
}
}
}
return false;
}
//Usage:
if(detectBot($_SERVER['HTTP_USER_AGENT'])) {
echo "You're A BOT";
} else {
echo "You're NOT A BOT";
}
@felixjet
Copy link

replace if(is_array($crawlers) {

with if(is_array($crawlers)) {

;) thanks.

@Exadra37
Copy link

Exadra37 commented Mar 9, 2014

Hi,

Avoid the foreach, because is will impact the performance, instead use a preg_match().

Take a look here https://gist.github.com/Exadra37/9453909

@Aziz-JH
Copy link

Aziz-JH commented Feb 4, 2015

In line 6, you forgot a bracket. ;)

@raza-hasnain
Copy link

raza-hasnain commented Jun 1, 2016

Nice
is there any option to detect bad bots ?
I am running a website, I get thousands of spammers on my website : www.funkodia.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment