2018-06-12 14:33:34 1298瀏覽
本篇文章小編主要和大家分享一下扣丁學(xué)堂PHP培訓(xùn)之PHP記錄搜索引擎爬行記錄的實(shí)現(xiàn)代碼,文章中會有代碼列出供大家參考學(xué)習(xí),對PHP開發(fā)感興趣的小伙伴就隨小編一起來了解一下吧。
下面是完整代碼:
//記錄搜索引擎爬行記錄 $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處理一下,將數(shù)據(jù)存儲于變量$tlc_thispage中。
fopen打開robotslogs.txt文件,將數(shù)據(jù)通過函數(shù)fwrite寫入,在通過函數(shù)fclose關(guān)閉就可以了。
因?yàn)槲矣X得沒必要,所以把自己網(wǎng)站上的代碼刪除了,所以也沒有效果示例了。
PS:php獲取各搜索蜘蛛爬行記錄的代碼
支持如下的搜索引擎:Baidu,Google,Bing,Yahoo,Soso,Sogou,Yodao爬行網(wǎng)站的記錄!
代碼:
<?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); } ?>
以上就是扣丁學(xué)堂PHP在線學(xué)習(xí)小編給大家分享的PHP記錄搜索引擎爬行記錄的實(shí)現(xiàn)代碼,希望對小伙伴們有所幫助,想要了解更多內(nèi)容的小伙伴可以登錄扣丁學(xué)堂官網(wǎng)咨詢??鄱W(xué)堂是專業(yè)的PHP培訓(xùn)機(jī)構(gòu),不僅有專業(yè)的老師和與時俱進(jìn)的課程體系,還有大量的PHP在線教程供學(xué)員掛看學(xué)習(xí)哦。扣丁學(xué)堂PHP技術(shù)交流群:374332265。
【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“php培訓(xùn)資訊”的相關(guān)文章>>