【发布时间】:2016-03-17 20:41:32
【问题描述】:
我有一个名为 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;
【问题讨论】:
-
您是否收到任何错误消息(例如
welcome.php未找到)? -
究竟是什么“没用”。您收到 PHP 错误消息了吗?你得到了意想不到的结果吗?如果是这样,你想要什么结果?例如,对于用户代理中带有“Twitterbot”的请求,我期望为真,但结果为假。帮助我们帮助您。
-
我会使用 stripos 并检查返回值是否 > 0 甚至 >= 0。您是否已经考虑过 robots.txt ?
-
@BareNakedCoder 我没有收到任何错误,因为服务器正在跳过该功能
-
你是如何测试它的?您是否尝试打印出
$_SERVER['HTTP_USER_AGENT']以确保您要搜索的字符串在其中?