通過PHP程序知道蜘蛛是否訪問你的網站(附代碼)
來源:程序員人生 發布時間:2014-06-13 06:42:43 閱讀次數:2424次
搜索引擎的蜘蛛訪問網站是通過遠程抓取頁面來進行的,我們不能使用JS代碼來取得蜘蛛的Agent信息,但是我們可以通過image標簽,這樣我們就可以得到蜘蛛的agent資料了,通過對agent資料的分析,就可以確定蜘蛛的種類、性別等因素,我們在通過數據庫或者文本來記錄就可以進行統計了。
數據庫結構:
- #
- # 表的結構 `naps_stats_bot`
- #
-
- CREATE TABLE `naps_stats_bot` (
- `botid` int(10) unsigned NOT NULL auto_increment,
- `botname` varchar(100) NOT NULL default '',
- `botagent` varchar(200) NOT NULL default '',
- `bottag` varchar(100) NOT NULL default '',
- `botcount` int(11) NOT NULL default '0',
- `botlast` datetime NOT NULL default '0000-00-00 00:00:00',
- `botlasturl` varchar(250) NOT NULL default '',
- UNIQUE KEY `botid` (`botid`),
- KEY `botname` (`botname`)
- ) TYPE=MyISAM AUTO_INCREMENT=9 ;
-
- #
- # 導出表中的數據 `naps_stats_bot`
- #
-
- 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', '');
- 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', '');
- INSERT INTO `naps_stats_bot` VALUES (3, 'Inktomi Slurp', 'Slurp/2.0', 'slurp', 0, '0000-00-00 00:00:00', '');
- INSERT INTO `naps_stats_bot` VALUES (4, 'Baiduspider', 'Baiduspider ( http://www.baidu.com/search/spider.htm)', 'baiduspider', 0, '0000-00-00 00:00:00', '');
- 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', '');
- INSERT INTO `naps_stats_bot` VALUES (6, 'Sohubot', 'sohu-search', 'sohu-search', 0, '0000-00-00 00:00:00', '');
- INSERT INTO `naps_stats_bot` VALUES (7, 'Lycos', 'Lycos/x.x', 'lycos', 0, '0000-00-00 00:00:00', '');
- INSERT INTO `naps_stats_bot` VALUES (8, 'Robozilla', 'Robozilla/1.0', 'robozilla', 0, '0000-00-00 00:00:00', '');
PHP程序:
- error_reporting(E_ALL & ~E_NOTICE);
-
- 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;
- }
- $tlc_thispage = addslashes($_SERVER['HTTP_USER_AGENT']);
-
- $searchbot = get_naps_bot();
- if ($searchbot) {
- $DB_naps->query("UPDATE naps_stats_bot SET botcount=botcount 1, botlast=NOW(), botlasturl='$tlc_thispage' WHERE botname='$searchbot'");
- }
- ?>
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
------分隔線----------------------------
------分隔線----------------------------