最近看群裡很多人在用 WordPress 蜘蛛爬行記錄生成這個功能,出於對網站最佳化的研究,清楚的瞭解蜘蛛爬行規律也是很重要的,我們就可以知道蜘蛛的爬行規律,根據這個規律我們再來寫文章,這樣符合了蜘蛛的胃口是很利於最佳化的。。。但是小編一直提倡著遠離外掛,熱愛速度的口號,所以今天就教大家用程式碼完美實現 WordPress 蜘蛛爬行記錄生成功能!

WordPress 主題檔案目錄下找到 function 檔案,用程式碼編輯軟體開啟,在<?php ?> 內的任意位置增加下面一段程式碼即可完成 WordPress 蜘蛛爬行記錄生成的功能!

  1. function get_naps_bot(){  
  2. $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);  
  3. if (strpos($useragent, 'googlebot') !== false){  
  4. return 'Googlebot';  
  5. }  
  6. if (strpos($useragent, 'msnbot') !== false){  
  7. return 'MSNbot';  
  8. }  
  9. if (strpos($useragent, 'slurp') !== false){  
  10. return 'Yahoobot';  
  11. }  
  12. if (strpos($useragent, 'baiduspider') !== false){  
  13. return 'Baiduspider';  
  14. }  
  15. if (strpos($useragent, 'sohu-search') !== false){  
  16. return 'Sohubot';  
  17. }  
  18. if (strpos($useragent, 'lycos') !== false){  
  19. return 'Lycos';  
  20. }  
  21. if (strpos($useragent, 'robozilla') !== false){  
  22. return 'Robozilla';  
  23. }  
  24. return false;  
  25. }  
  26. function nowtime(){  
  27. date_default_timezone_set('Asia/Shanghai');  
  28. $date=date("Y-m-d.G:i:s");  
  29. return $date;  
  30. }  
  31. $searchbot = get_naps_bot();  
  32. if ($searchbot) {  
  33. $tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']);  
  34. $url=$_SERVER['HTTP_REFERER'];  
  35. $file="robotslogs.txt";  
  36. $time=nowtime();  
  37. $data=fopen($file,"a");  
  38. $PR="$_SERVER[REQUEST_URI]";  
  39. fwrite($data,"Time:$time robot:$searchbot URL:$tlc_thispage
     page:$PR
    "
    );  
  40. fclose($data);  
  41. }  

使用上面這段程式碼後,最好在網站的根目錄建立個 robotslogs.txt 的檔案,並且 linux 主機設定許可權為 777,然後過段時間,訪問 http://網址/robotslogs.txt 即可看到蜘蛛爬行記錄了!

一些說使用後出錯的朋友,請研究下 function 檔案內,插入的位置!