首頁  >  文章  >  後端開發  >  php記錄搜尋引擎爬行過程方法詳解

php記錄搜尋引擎爬行過程方法詳解

php中世界最好的语言
php中世界最好的语言原創
2018-05-18 14:17:541448瀏覽

這次帶給大家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[&#39;HTTP_USER_AGENT&#39;]); 
if (strpos($useragent, &#39;googlebot&#39;) !== false){ 
return &#39;Google&#39;; 
} 
if (strpos($useragent, &#39;baiduspider&#39;) !== false){ 
return &#39;Baidu&#39;; 
} 
if (strpos($useragent, &#39;msnbot&#39;) !== false){ 
return &#39;Bing&#39;; 
} 
if (strpos($useragent, &#39;slurp&#39;) !== false){ 
return &#39;Yahoo&#39;; 
} 
if (strpos($useragent, &#39;sosospider&#39;) !== false){ 
return &#39;Soso&#39;; 
} 
if (strpos($useragent, &#39;sogou spider&#39;) !== false){ 
return &#39;Sogou&#39;; 
} 
if (strpos($useragent, &#39;yodaobot&#39;) !== false){ 
return &#39;Yodao&#39;; 
} 
return false; 
} 
function nowtime(){ 
$date=date("Y-m-d.G:i:s"); 
return $date; 
} 
$searchbot = get_naps_bot(); 
if ($searchbot) { 
$tlc_thispage = addslashes($_SERVER[&#39;HTTP_USER_AGENT&#39;]); 
$url=$_SERVER[&#39;HTTP_REFERER&#39;]; 
$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對象的值

以上是php記錄搜尋引擎爬行過程方法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn