本文主要和大家介紹了php記錄搜尋引擎爬行記錄的實現代碼,然後在文中給大家補充介紹了php擷取各搜尋蜘蛛爬行記錄的代碼,需要的朋友可以參考下,希望能協助到大家。
下面是完整代碼:
//記錄搜尋引擎爬行記錄 $searchbot = get_naps_bot(); if ($searchbot) { $tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']); $url = $_SERVER['HTTP_REFERER']; $file = WEB_PATH.'robotslogs.txt'; $date = date('Y-m-d H:i:s'); $data = fopen($file,'a'); fwrite($data,"Time:$date robot:$searchbot URL:$tlc_thispage/r/n"); fclose($data);}
WEB_PATH為index.PHP下define的根目錄路徑,意思就是說robotslogs.txt檔案是放在根目錄下的。
通過get_naps_bot()擷取蜘蛛爬行記錄,然後在通過addslashes處理一下,將資料存放區於變數$tlc_thispage中。
fopen開啟robotslogs.txt檔案,將資料通過函數fwrite寫入,在通過函數fclose關閉就可以了。
因為我覺得沒必要,所以把自己網站上的代碼刪除了,所以也沒有效果樣本了。
PS:php擷取各搜尋蜘蛛爬行記錄的代碼
支援如下的搜尋引擎:Baidu,Google,Bing,Yahoo,Soso,Sogou,Yodao爬行網站的記錄!
代碼:
<?php /*** 擷取搜尋引擎爬行記錄* edit by www.jb51.net*/function get_naps_bot() { $useragent = strtolower($_SERVER['HTTP_USER_AGENT']); if (strpos($useragent, 'googlebot') !== false){ return 'Google'; } if (strpos($useragent, 'baiduspider') !== false){ return 'Baidu'; } if (strpos($useragent, 'msnbot') !== false){ return 'Bing'; } if (strpos($useragent, 'slurp') !== false){ return 'Yahoo'; } if (strpos($useragent, 'sosospider') !== false){ return 'Soso'; } if (strpos($useragent, 'sogou spider') !== false){ return 'Sogou'; } if (strpos($useragent, 'yodaobot') !== false){ return 'Yodao'; } return false; } function nowtime(){ $date=date("Y-m-d.G:i:s"); return $date; } $searchbot = get_naps_bot(); if ($searchbot) { $tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']); $url=$_SERVER['HTTP_REFERER']; $file="www.jb51.net.txt"; $time=nowtime(); $data=fopen($file,"a"); fwrite($data,"Time:$time robot:$searchbot URL:$tlc_thispage\n"); fclose($data); } ?>