最近看群裡很多人在用 WordPress 蜘蛛爬行記錄生成這個功能,出於對網站最佳化的研究,清楚的瞭解蜘蛛爬行規律也是很重要的,我們就可以知道蜘蛛的爬行規律,根據這個規律我們再來寫文章,這樣符合了蜘蛛的胃口是很利於最佳化的。。。但是小編一直提倡著遠離外掛,熱愛速度的口號,所以今天就教大家用程式碼完美實現 WordPress 蜘蛛爬行記錄生成功能!
在 WordPress 主題檔案目錄下找到 function 檔案,用程式碼編輯軟體開啟,在<?php ?> 內的任意位置增加下面一段程式碼即可完成 WordPress 蜘蛛爬行記錄生成的功能!
- function get_naps_bot(){
- $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
- if (strpos($useragent, 'googlebot') !== false){
- return 'Googlebot';
- }
- if (strpos($useragent, 'msnbot') !== false){
- return 'MSNbot';
- }
- if (strpos($useragent, 'slurp') !== false){
- return 'Yahoobot';
- }
- if (strpos($useragent, 'baiduspider') !== false){
- return 'Baiduspider';
- }
- if (strpos($useragent, 'sohu-search') !== false){
- return 'Sohubot';
- }
- if (strpos($useragent, 'lycos') !== false){
- return 'Lycos';
- }
- if (strpos($useragent, 'robozilla') !== false){
- return 'Robozilla';
- }
- return false;
- }
- function nowtime(){
- date_default_timezone_set('Asia/Shanghai');
- $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="robotslogs.txt";
- $time=nowtime();
- $data=fopen($file,"a");
- $PR="$_SERVER[REQUEST_URI]";
- fwrite($data,"Time:$time robot:$searchbot URL:$tlc_thispage
page:$PR
"); - fclose($data);
- }
使用上面這段程式碼後,最好在網站的根目錄建立個 robotslogs.txt 的檔案,並且 linux 主機設定許可權為 777,然後過段時間,訪問 http://網址/robotslogs.txt 即可看到蜘蛛爬行記錄了!
一些說使用後出錯的朋友,請研究下 function 檔案內,插入的位置!