我有一个名为 index.php 的页面和一个用于检测机器人的脚本,但它无法正常工作。如果机器人访问 index.php,那么我希望包含 welcome.php。如果是原始用户,则不应包含 welcome.php。 到目前为止,这是我尝试过的:
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
}
这是我遇到的主要问题 - 以下方法无效:
if (is_bot()==true) {
session_destroy(); include_once('welcome.php'); exit; }
接下来,我尝试了这个,但也没有用:
if (is_bot()) {
session_destroy(); include_once('welcome.php'); exit; }
请告知针对这种情况的任何解决方案。
每当我这样使用时,它就有效
if (is_bot())
$isbot = 1;
else
$isbot = 0;
最佳答案
最好改进您的 is_bot 函数并使用正则表达式来代替漫长而忙碌的搜索。
像下面这样的东西可能更有用。
function is_bot(){
preg_match('/bot|curl|spider|google|twitter^$/i', $_SERVER['HTTP_USER_AGENT'], $matches);
return (empty($matches)) ? false : true;
}
关于php - 使用 php 检测机器人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34231938/