使用php检测bot

2 php bots

我有一个名为index.php的页面,并且有一个用于检测该bot的脚本,但是它无法正常工作。如果该机器人访问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
    }
Run Code Online (Sandbox Code Playgroud)

这是我遇到的主要问题-以下内容无效:

  if (is_bot()==true) {
session_destroy(); include_once('welcome.php'); exit; }
Run Code Online (Sandbox Code Playgroud)

接下来,我尝试了此方法,但它也没有起作用:

  if (is_bot()) {
  session_destroy(); include_once('welcome.php'); exit; }
Run Code Online (Sandbox Code Playgroud)

请针对这种情况提供任何解决方案的建议。

每当我这样使用时

 if (is_bot())
 $isbot = 1;
 else
 $isbot = 0;
Run Code Online (Sandbox Code Playgroud)

Imr*_*oor 5

最好改善is_bot函数,并使用正则表达式代替长时间忙碌的搜索。

如下所示可能会更有用。

function is_bot(){
    preg_match('/bot|curl|spider|google|twitter^$/i', $_SERVER['HTTP_USER_AGENT'], $matches);

    return (empty($matches)) ? false : true;
}
Run Code Online (Sandbox Code Playgroud)