Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. function detectSearchBot($ip, &$hostname)
  2.     {
  3.         // success - return host
  4.         // fail - return ip or false
  5.         $hostname = gethostbyaddr($ip);
  6. //        $hostname = '93-158-166-6.spider.yandex.com';
  7. //        $hostname = '93-158-166-6.spider.yandex.ru';
  8. //        $hostname = '93-158-166-6.spider.yan3dex.ru';
  9. //        $hostname = 'crawl-66-249-79-95.googlebot.com';
  10. //        $hostname = 'crawl-66-249-79-95.google.com';
  11.  
  12.         // https://support.google.com/webmasters/answer/80553
  13.         if ($hostname !== false && $hostname != $ip) {
  14.             // detect google and yandex bots
  15.             $is_match = preg_match('/\.((?:google(?:bot)?|yandex)\.(?:com|ru))$/iu', $hostname);
  16.  
  17.             if ($is_match) {
  18.                 // success - return ip
  19.                 // fail - return hostname
  20.                 $ip = gethostbyname($hostname);
  21.  
  22.                 if ($ip != $hostname) {
  23.                     return true;
  24.                 }
  25.             }
  26.         }
  27.  
  28.         return false;
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement