thinkphp6記錄百度蜘蛛日志。在控制器的父類(lèi)如IndexBase中寫(xiě)入以下代碼,所有前端控制器繼承這個(gè)控制器
public function initialize()
{
parent::initialize(); // TODO: Change the autogenerated stub
if ($this->Config['web_status'] == 0) { // 判斷是否關(guān)閉網(wǎng)站
die('網(wǎng)站已經(jīng)關(guān)閉');
}
$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; // 不怕數(shù)據(jù)大的話(huà)可以取消注釋?zhuān)涗浰性L(fǎng)問(wèn)日志
}
if (!empty($title)) {
BaiduLog::create(["title"=>$title,"href"=>$url,"ip"=>$ip]);
}
}以上就是thinkphp6記錄百度蜘蛛爬行日志的方法,
get_real_ip()
是獲取客戶(hù)真實(shí)IP的自定義函數(shù),可以在本站搜索找到。

