首页 > php教程 > php手册 > 通过PHP程序知道蜘蛛是否访问你的网站(附代码)

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-06-02 09:14:26
原创
1503 人浏览过

搜索引擎的蜘蛛访问网站是通过远程抓取页面来进行的,我们不能使用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程序:

<?php
error_reporting(E_ALL & ~E_NOTICE);
function get_naps_bot() {
    $useragent = strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;]);
    if (strpos($useragent, &#39;googlebot&#39;) !== false) {
        return &#39;Googlebot&#39;;
    }
    if (strpos($useragent, &#39;msnbot&#39;) !== false) {
        return &#39;MSNbot&#39;;
    }
    if (strpos($useragent, &#39;slurp&#39;) !== false) {
        return &#39;Yahoobot&#39;;
    }
    if (strpos($useragent, &#39;baiduspider&#39;) !== false) {
        return &#39;Baiduspider&#39;;
    }
    if (strpos($useragent, &#39;sohu-search&#39;) !== false) {
        return &#39;Sohubot&#39;;
    }
    if (strpos($useragent, &#39;lycos&#39;) !== false) {
        return &#39;Lycos&#39;;
    }
    if (strpos($useragent, &#39;robozilla&#39;) !== false) {
        return &#39;Robozilla&#39;;
    }
    return false;
}
$tlc_thispage = addslashes($_SERVER[&#39;HTTP_USER_AGENT&#39;]);
//添加蜘蛛的抓取记录
$searchbot = get_naps_bot();
if ($searchbot) {
    $DB_naps->query("UPDATE naps_stats_bot SET botcount=botcount 1, botlast=NOW(), botlasturl=&#39;$tlc_thispage&#39; WHERE botname=&#39;$searchbot&#39;");
}
?>
登录后复制


本文地址:

转载随意,但请附上文章地址:-)

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
用php如何生成小程序的小程序码?
来自于 1970-01-01 08:00:00
0
0
0
php为什么是源程序显示?
来自于 1970-01-01 08:00:00
0
0
0
找精通PHP的修改程序,新手勿扰
来自于 1970-01-01 08:00:00
0
0
0
如何将prompt()输入的值传递给php程序
来自于 1970-01-01 08:00:00
0
0
0
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板