下面thinkphp框架教程栏目将给大家介绍ThinkPHP 6是怎么记录百度等蜘蛛爬取日志,希望对需要的朋友有所帮助!

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记录蜘蛛爬取日志的方法的详细内容,更多请关注Work网其它相关文章!

09-11 15:23