<button id="g80sy"></button>
  • <abbr id="g80sy"></abbr>
  • <button id="g80sy"></button>
    <rt id="g80sy"><delect id="g80sy"></delect></rt>
  • thinkphp6如何記錄百度等蜘蛛的爬取日志

    程序猿 2021-03-17 20:08:08 2090瀏覽 加載中

    thinkphp6記錄百度蜘蛛日志。在控制器的父類如IndexBase中寫入以下代碼,所有前端控制器繼承這個控制器

      public function initialize()
        {
    
            parent::initialize(); // TODO: Change the autogenerated stub
    
            if ($this->Config['web_status'] == 0) {  // 判斷是否關閉網站
                die('網站已經關閉');
            }
    
            $this->baiduLog();
        }
    
        protected function baiduLog()
        {
            $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
            $url = $this->request->controller() . "/" . $this->request->action();
            $param = input("param.","","htmlspecialchars");
            $url = (string) url($url,$param);
            $ip = get_real_ip();
            $title = "";
            if (strpos($useragent, 'googlebot') !== false){
                $title =  'Google';
            } elseif (strpos($useragent, 'baiduspider') !== false){
                $title =  'Baidu';
            } elseif (strpos($useragent, 'msnbot') !== false){
                $title =  'Bing';
            } elseif (strpos($useragent, 'slurp') !== false){
                $title =  'Yahoo';
            } elseif (strpos($useragent, 'sosospider') !== false){
                $title =  'Soso';
            } elseif (strpos($useragent, 'sogou spider') !== false){
                $title =  'Sogou';
            } elseif (strpos($useragent, 'yodaobot') !== false){
                $title =  'Yodao';
            } elseif (strpos($useragent, 'googlebot') !== false){
                $title =  'Google';
            } elseif (strpos($useragent, 'baiduspider') !== false){
                $title =  'Baidu';
            } else {
    //            $title = $useragent; // 不怕數據大的話可以取消注釋,記錄所有訪問日志
            }
            if (!empty($title)) {
                BaiduLog::create(["title"=>$title,"href"=>$url,"ip"=>$ip]);
            }
        }

    以上就是thinkphp6記錄百度蜘蛛爬行日志的方法,

    get_real_ip()

    是獲取客戶真實IP的自定義函數,可以在本站搜索找到。

    標簽: thinkphp6
    最后修改:2025-03-23 19:23:41

    非特殊說明,本博所有文章均為博主原創。

    主站蜘蛛池模板: 梅州市| 偏关县| 苍南县| 台东市| 临汾市| 泰安市| 大悟县| 南澳县| 时尚| 东源县| 江山市| 教育| 永城市| 抚顺市| 柘荣县| 双桥区| 延川县| 镇安县| 宜良县| 扶风县| 通州区| 黑河市| 墨江| 泾源县| 洪湖市| 双鸭山市| 开原市| 类乌齐县| 江源县| 库尔勒市| 绍兴县| 四川省| 东安县| 湘阴县| 驻马店市| 梓潼县| 攀枝花市| 贵定县| 安溪县| 康乐县| 宜宾市|