I do not allow suspicious bots to the site, I wrote a small script, but it doesn’t look very good, maybe there is another way to iterate through the array?

$user_agent = $_SERVER['HTTP_USER_AGENT']; $bot = array("MJ12bot","MSIE","AhrefsBot"); for ($i = 0; $i <= 2; $i++){ $check = strripos($user_agent, $bot[$i]); if ($check === false) { echo " "; } else { die('Боты, валите отсюда!'); } } 

    3 answers 3

    For example:

     $user_agent = $_SERVER['HTTP_USER_AGENT']; $bots = array("MJ12bot","MSIE","AhrefsBot"); foreach ($bots as $bot){ if (!strripos($user_agent, $bot)) die('Боты, валите отсюда!'); } } 

      Instead of i <= 2 put i < count($bot) or i < sizeof($bot) - then you can not change 2 for the number of elements in the array -1. Or you can simply use foreach($bot as $str) and then in each iteration the name of the bot will be in $str

        It might be easier to close via robots.txt:

         User-agent: AhrefsBot Disallow: / User-agent: MJ12bot Disallow: / 

        Ahrefs normal bot, will take into account robots . About MJ12bot not sure, but most likely it will be. MSIE incomprehensible, most likely he does not care about the rules.

        Well, remember that if desired, bots can always introduce themselves as a normal user agent. So here already look at the ip and the number of requests.