I have a page called index.php and I have a script for detecting the bot, but it's not working properly. If the bot visits index.php, then I want welcome.php to be included. If it's the original user, then welcome.php shouldn't be included. This is what I have tried so far:
function is_bot(){
$botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi",
"looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory",
"Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot",
"crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp",
"msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz",
"Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot",
"Mediapartners-Google", "Sogou web spider", "WebAlta
Crawler","TweetmemeBot", "Butterfly", "Twitturls", "Me.dium",
"Twiceler", "Purebot", "facebookexternalhit",
"Yandex", "CatchBot", "W3C_Validator", "Jigsaw","PostRank",
"Purebot", "Twitterbot",
"Voyager", "zelist", "pingdom", "favicon");
foreach($botlist as $bot){
if(strpos($_SERVER['HTTP_USER_AGENT'],$bot)!==false)
return true; // Is a bot
}
return false; // Not a bot
}
Here is the main problem I'm getting - the following didn't work:
if (is_bot()==true) {
session_destroy(); include_once('welcome.php'); exit; }
Next, I tried this, but it also didn't work:
if (is_bot()) {
session_destroy(); include_once('welcome.php'); exit; }
Please advise on any solutions for this situation.
Whenever i use like this it works
if (is_bot())
$isbot = 1;
else
$isbot = 0;
I'm pretty certain the issue is that the code does work (although is poorly optimised and formatted - @Imran's solution is much cleaner) but you are testing it incorrectly.
Your UA string doesn't contain the "bot" string - you are not a server. Use the Google Chrome dev tool, like so;
F12
CTRL + SHIFT + M
UA box at the top and alter your UA string to pretend to be somebody else e.g. "Googlebot" and then test it.
Just by visiting a website and navigating back to your's does not imitate a 'bot request' from that website, it is still just you!
It's better to improve your is_bot function and use regular expression instead the long hectic search.
Something like below can be more usefull.
function is_bot(){
preg_match('/bot|curl|spider|google|twitter^$/i', $_SERVER['HTTP_USER_AGENT'], $matches);
return (empty($matches)) ? false : true;
}