S
S
Sergey2015-02-18 12:38:05
PHP
Sergey, 2015-02-18 12:38:05

How to determine that the search bot and not a person?

There is a module that determines the city and transfers to the subdomain belonging to this city. A search engine (Yandex) visits the site and is transferred to Moscow.
I think it is necessary to check if the visitor is a bot and if not, then do not redirect him.
How can you accurately determine that a search engine has visited the site?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Evgeny Kalibrov, 2015-02-18
@gangstarcj

Here is a solution for all known bots:

function isBot(&$botname = ''){
/* Эта функция будет проверять, является ли посетитель роботом поисковой системы */
  $bots = array(
    'rambler','googlebot','aport','yahoo','msnbot','turtle','mail.ru','omsktele',
    'yetibot','picsearch','sape.bot','sape_context','gigabot','snapbot','alexa.com',
    'megadownload.net','askpeter.info','igde.ru','ask.com','qwartabot','yanga.co.uk',
    'scoutjet','similarpages','oozbot','shrinktheweb.com','aboutusbot','followsite.com',
    'dataparksearch','google-sitemaps','appEngine-google','feedfetcher-google',
    'liveinternet.ru','xml-sitemaps.com','agama','metadatalabs.com','h1.hrn.ru',
    'googlealert.com','seo-rus.com','yaDirectBot','yandeG','yandex',
    'yandexSomething','Copyscape.com','AdsBot-Google','domaintools.com',
    'Nigma.ru','bing.com','dotnetdotcom'
  );
  foreach($bots as $bot)
    if(stripos($_SERVER['HTTP_USER_AGENT'], $bot) !== false){
      $botname = $bot;
      return true;
    }
  return false;
}

Usage:
if( isBot($bname) ) {
   // Перебрасываем на Москву
}

F
FanatPHP, 2015-02-18
@FanatPHP

I think that it is necessary not to throw, but to ASK .
How sick of such unfortunate dropouts who know better than me what city I live in, in what language I want to see the site ...

A
Anton Shamov, 2015-04-12
@Mulfarion

Taken out of context.
xdan.ru/How-to-detect-on-PHP-if-a-page-is-visited-...
But this is so by the way. But the logic is not clear.
Drawn here $botname = $bot; if the function returns true or false? Looks like from somewhere else clumsy little hands tore the function. So it requires digestion for the codec to work. But the main thing is the idea, but how to twist it is clear.

L
l2sap, 2022-01-03
@l2sap

Thanks to Evgeny Kalibrov for the script. Been using this script for a while. Later I decided to simplify it. My script is universal, it catches any robot: Google, Yandex, Yahoo and Bing. It is convenient because you do not need to list each robot separately, and here we do without loops, which works several times faster. Checked, finds bots perfectly! Try it and use it to your health.

class ScanBot
{

    public function isBot()
    {

        $down_reg = mb_strtolower($_SERVER['HTTP_USER_AGENT']);
        $down_reg = str_replace(array(' '), '', $down_reg);

        $find_bot_step1 = str_replace(array('yandex', 'google', 'yahoo', 'bing'), ' this_is_bot! ', $down_reg);
        $find_bot_step2 = explode(' ', $find_bot_step1);

        if ($find_bot_step2[1] == "this_is_bot!") {
            return true;
        }

        return false;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question