Technology
Simple PHP Function To Detect Bots And Crawlers With Full Explanation !!!
Hello Friends,
Today I am here with a simple php function which can be used to Detect common Bots and Crawlers.
The previous night while fixing an Ad network script I got a place where I need to rewrite the code with Bots detection function, and I made a simple function to detect common Bots and decided to share it with you all.
Common eco-friendly Bots are easy to detect, You need not to know any rocket science in it.
HERE IS THE FUNCTION I HAVE WRITTEN :
[code language=”php”]<?php
function detectBot($USER_AGENT)
{ $crawlers_agents = array(
‘Bloglines subscribe’,
‘DumbotSosoimagespider’,
‘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’);
if(is_array($crawlers_agents))
{ foreach($crawlers_agents as $crawler)
{ if (strpos(strtolower($USER_AGENT),trim($crawler)) !== false)
{ return true; } } }
return false; }
How To Use?
[code language=”php”]<?php
if(detectBot($_SERVER[‘HTTP_USER_AGENT’]))
{ echo "Sorry To Say But You’re A BOT"; }
else
{ echo "You’re NOT A BOT"; }
Preview:
You're NOT A BOT
How Does It Work?
At first, I’ve made a list of the user agent of common Bots then made an array of them,
Now I just Tried to match the user agent of the visitor with the one in the array.
If the user agent machetes, the visitor probably a Bot, else a normal user.
Hello. 2 thoughts. First you don’t need to verify if $crawlers_agents is an array since you just declare it and second, $crawlers on line 50 should be $crawlers_agents. Thank you for the function.
Thanks for your reply! I have updated the post.
Thank You so much. 🙂