php - 使用 php 检测机器人

标签 php bots

我有一个名为 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/

相关文章:

c - 用 C 语言编写的 IRC 机器人

php - Laravel Eloquent : Inverse of Many To Many (Polymorphic)

php - 谷歌将 MySQL lat lng 映射到 JavaScript

php - Monolog:捕获错误/异常并在响应中输出消息的处理程序(根据PHP默认值)

php session id值到多个页面

php - PDO::query() 期望参数 1 为字符串,对象给出警告

python - 尝试运行命令 'ctx is a required argument that is missing' 时收到错误。 [Python, discord.py]

bots - 微软机器人框架 : How to define the image path which exists in the solution itself

client - on_message 停止我所有的 bot.commands 工作

php - 通过 PHP 机器人在 IRC 上发送粗体/彩色消息