最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

通过PHP程序知道蜘蛛是否访问你的网站(附代码

时间:2014-06-09 16:39来源: 作者: 点击: 次
搜索引擎的蜘蛛访问网站是通过远程抓取页面来进行的,我们不能使用JS代码来取得蜘蛛的Agent信息,但是我们可以通过image标签,这样我们就可以得到蜘蛛的agent资料了,通过对agent资料的分析

搜索引擎的蜘蛛访问网站是通过远程抓取页面来进行的,我们不能使用JS代码来取得蜘蛛的Agent信息,但是我们可以通过image标签,这样我们就可以得到蜘蛛的agent资料了,通过对agent资料的分析,就可以确定蜘蛛的种类、性别等因素,我们在通过数据库或者文本来记录就可以进行统计了。

数据库结构:

  1. # 
  2. # 表的结构 `naps_stats_bot` 
  3. # 
  4.  
  5. CREATE TABLE `naps_stats_bot` ( 
  6. `botid` int(10) unsigned NOT NULL auto_increment, 
  7. `botname` varchar(100) NOT NULL default '', 
  8. `botagent` varchar(200) NOT NULL default '', 
  9. `bottag` varchar(100) NOT NULL default '', 
  10. `botcount` int(11) NOT NULL default '0', 
  11. `botlast` datetime NOT NULL default '0000-00-00 00:00:00', 
  12. `botlasturl` varchar(250) NOT NULL default '', 
  13. UNIQUE KEY `botid` (`botid`), 
  14. KEY `botname` (`botname`) 
  15. ) TYPE=MyISAM AUTO_INCREMENT=9 ; 
  16.  
  17. # 
  18. # 导出表中的数据 `naps_stats_bot` 
  19. # 
  20.  
  21. INSERT INTO `naps_stats_bot` VALUES (1, 'Googlebot', 'Googlebot/2.X ( http://www.googlebot.com/bot.html)', 'googlebot', 0, '0000-00-00 00:00:00', ''); 
  22. INSERT INTO `naps_stats_bot` VALUES (2, 'MSNbot', 'MSNBOT/0.1 (http://search.msn.com/msnbot.htm)', 'msnbot', 0, '0000-00-00 00:00:00', ''); 
  23. INSERT INTO `naps_stats_bot` VALUES (3, 'Inktomi Slurp', 'Slurp/2.0', 'slurp', 0, '0000-00-00 00:00:00', ''); 
  24. INSERT INTO `naps_stats_bot` VALUES (4, 'Baiduspider', 'Baiduspider ( http://www.baidu.com/search/spider.htm)', 'baiduspider', 0, '0000-00-00 00:00:00', ''); 
  25. INSERT INTO `naps_stats_bot` VALUES (5, 'Yahoobot', 'Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)', 'slurp', 0, '0000-00-00 00:00:00', ''); 
  26. INSERT INTO `naps_stats_bot` VALUES (6, 'Sohubot', 'sohu-search', 'sohu-search', 0, '0000-00-00 00:00:00', ''); 
  27. INSERT INTO `naps_stats_bot` VALUES (7, 'Lycos', 'Lycos/x.x', 'lycos', 0, '0000-00-00 00:00:00', ''); 
  28. INSERT INTO `naps_stats_bot` VALUES (8, 'Robozilla', 'Robozilla/1.0', 'robozilla', 0, '0000-00-00 00:00:00', ''); 

PHP程序:

  1. error_reporting(E_ALL & ~E_NOTICE); 
  2.  
  3. function get_naps_bot() 
  4. { 
  5. $useragent = strtolower($_SERVER['HTTP_USER_AGENT']); 
  6.  
  7. if (strpos($useragent, 'googlebot') !== false){ 
  8. return 'Googlebot'; 
  9. } 
  10.  
  11. if (strpos($useragent, 'msnbot') !== false){ 
  12. return 'MSNbot'; 
  13. } 
  14.  
  15. if (strpos($useragent, 'slurp') !== false){ 
  16. return 'Yahoobot'; 
  17. } 
  18.  
  19. if (strpos($useragent, 'baiduspider') !== false){ 
  20. return 'Baiduspider'; 
  21. } 
  22.  
  23. if (strpos($useragent, 'sohu-search') !== false){ 
  24. return 'Sohubot'; 
  25. } 
  26.  
  27. if (strpos($useragent, 'lycos') !== false){ 
  28. return 'Lycos'; 
  29. } 
  30.  
  31. if (strpos($useragent, 'robozilla') !== false){ 
  32. return 'Robozilla'; 
  33. } 
  34. return false; 
  35. } 
  36. $tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']); 
  37. //添加蜘蛛的抓取记录 
  38. $searchbot = get_naps_bot(); 
  39. if ($searchbot) { 
  40. $DB_naps->query("UPDATE naps_stats_bot SET botcount=botcount 1, botlast=NOW(), botlasturl='$tlc_thispage' WHERE botname='$searchbot'"); 
  41. } 
  42. ?> 
(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容