这篇文章主要介绍“ThinkPHP6记录蜘蛛爬取日志的方法是什么”,在日常操作中,相信很多人在ThinkPHP6记录蜘蛛爬取日志的方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”ThinkPHP6记录蜘蛛爬取日志的方法是什么”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
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记录蜘蛛爬取日志的方法是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。