PHP检查搜索引擎蜘蛛爬虫真实性

180it 2020-02-27 PM 2011℃ 0条
<?php
if(getspider()){
    exit('假的搜索引擎!');
}
function getspider(){
    $访问UA = strtolower($_SERVER['HTTP_USER_AGENT']);
      $访问IP = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
      if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
          $list = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
          $访问IP = $list[0];
      }
      if (!ip2long($访问IP)) $访问IP = '未知IP';
    $访问主机 = str_replace('.', '-', $访问IP);
    if (strpos($访问UA, 'googlebot') !== false){
        $解析主机 = gethostbyaddr($_SERVER['REMOTE_ADDR']);
        if ($解析主机 != 'crawl-'.$访问主机.'.googlebot.com') return 'googlebot?';
    }

    if (strpos($访问UA, 'sogou') !== false){
        $解析主机 = gethostbyaddr($_SERVER['REMOTE_ADDR']);
        if ($解析主机 != 'sogouspider-'.$访问主机.'.crawl.sogou.com') return 'sogou';
    }
    if (strpos($useragent, 'bing') !== false){
        $解析主机 = gethostbyaddr($_SERVER['REMOTE_ADDR']);
        if ($解析主机 != 'msnbot-'.$访问主机.'.search.msn.com') return 'bing';
    }
    if (strpos($访问UA, 'baiduspider') !== false){
        $解析主机 = gethostbyaddr($_SERVER['REMOTE_ADDR']);
        if ($解析主机 != 'baiduspider-'.$访问主机.'.crawl.baidu.com') return 'baiduspider?';
    }
    return false;
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

PHP检查搜索引擎蜘蛛爬虫真实性