监控爬虫的超级精简的类
时间:2011-07-12
来源:互联网
<?php
/*********************************************************************
* 超级简单的判断是否有机器人蜘蛛来抓取网页的类
* 来路: www.sgcha.cn
* 看看就行
****************************************************************/
class BotIdentifier {
protected $arrstrBotMatches;
function __construct() {
//爬虫的数组
$arrstrBots = array (
'googlebot' => '/Googlebot/',
'msnbot' => '/MSNBot/',
'yahoo' => '/Yahoo/',
'baidu'
=> '/Baiduspider/' //后面要什么自己加就是了
);
//其实就是通过变量HTTP_USER_AGENT
if( true == isset( $_SERVER['HTTP_USER_AGENT'] )) {
$this->arrstrBotMatches = preg_filter( $arrstrBots, array_fill( 1, count( $arrstrBots ), '$0' ), array( trim( $_SERVER['HTTP_USER_AGENT'] )));
}
}
function getBot() {
//判断是否为bot
return ( true == is_array( $this->arrstrBotMatches ) && 0 < count( $this->arrstrBotMatches )) ? $this->arrstrBotMatches[0] : '不是爬虫';
}
function isBot() {
return ( true == is_array( $this->arrstrBotMatches ) && 0 < count( $this->arrstrBotMatches )) ? 1 : 0;
}
}
//$_SERVER['HTTP_USER_AGENT'] = 'Googlebot/2.1 (+http://www.googlebot.com/bot.html)';
$objBot = new BotIdentifier();
echo '<b>呵呵用的什么破浏览器 </b>' . $_SERVER['HTTP_USER_AGENT'];
echo '<br><b>是否是爬虫: </b>' . $boolIsBot = $objBot->isBot();
echo '<br><b>我日,什么虫: </b>' . $strBot = $objBot->getBot();
?>
/*********************************************************************
* 超级简单的判断是否有机器人蜘蛛来抓取网页的类
* 来路: www.sgcha.cn
* 看看就行
****************************************************************/
class BotIdentifier {
protected $arrstrBotMatches;
function __construct() {
//爬虫的数组
$arrstrBots = array (
'googlebot' => '/Googlebot/',
'msnbot' => '/MSNBot/',
'yahoo' => '/Yahoo/',
'baidu'
=> '/Baiduspider/' //后面要什么自己加就是了
);
//其实就是通过变量HTTP_USER_AGENT
if( true == isset( $_SERVER['HTTP_USER_AGENT'] )) {
$this->arrstrBotMatches = preg_filter( $arrstrBots, array_fill( 1, count( $arrstrBots ), '$0' ), array( trim( $_SERVER['HTTP_USER_AGENT'] )));
}
}
function getBot() {
//判断是否为bot
return ( true == is_array( $this->arrstrBotMatches ) && 0 < count( $this->arrstrBotMatches )) ? $this->arrstrBotMatches[0] : '不是爬虫';
}
function isBot() {
return ( true == is_array( $this->arrstrBotMatches ) && 0 < count( $this->arrstrBotMatches )) ? 1 : 0;
}
}
//$_SERVER['HTTP_USER_AGENT'] = 'Googlebot/2.1 (+http://www.googlebot.com/bot.html)';
$objBot = new BotIdentifier();
echo '<b>呵呵用的什么破浏览器 </b>' . $_SERVER['HTTP_USER_AGENT'];
echo '<br><b>是否是爬虫: </b>' . $boolIsBot = $objBot->isBot();
echo '<br><b>我日,什么虫: </b>' . $strBot = $objBot->getBot();
?>
作者: 大水车 发布时间: 2011-07-12
我有的时候觉得把简单的功能封装成类也不见的就比面向过程好多少,反而更累赘了,面向过程只要设计的合理一样是易维护可复用高扩展
作者: qxhy123 发布时间: 2011-07-12
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28