首页 / php开发 / ThinkPHP6记录百度等蜘蛛的爬取日志

ThinkPHP6记录百度等蜘蛛的爬取日志

2022-06-12 06:12 php开发 阅读 6631 来源

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的自定义函数。

猜你喜欢