這次給大家帶來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); } ?>
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
推薦閱讀:
php讀取本地json檔案有哪些方法
php有哪些方法可以輸出json對象的值