php判断爬虫
function checkrobot($useragent = ''){
static $kw_spiders = 'Bot|Crawl|Spider|slurp|sohu-search|lycos|robozilla';
static $kw_browsers = 'MSIE|Netscape|Opera|Konqueror|Mozilla';
$useragent = empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent;
if(!strexists($useragent, 'http://') && preg_match("/($kw_browsers)/i", $useragent)) {
return false;
} elseif(preg_match("/($kw_spiders)/i", $useragent)) {
return true;
} else {
return false;
}
}
function strexists($haystack, $needle) {
return !(strpos($haystack, $needle) === FALSE);
}
if(checkrobot()){
echo '机器人爬虫';
}else{
echo '人';
}
?>
<?php
function checkrobot($useragent=''){
static $kw_spiders = array('bot', 'crawl', 'spider' ,'slurp', 'sohu-search', 'lycos', 'robozilla');
static $kw_browsers = array('msie', 'netscape', 'opera', 'konqueror', 'mozilla');
$useragent = strtolower(empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent);
if(strpos($useragent, 'http://') === false && dstrpos($useragent, $kw_browsers)) return false;
if(dstrpos($useragent, $kw_spiders)) return true;
return false;
}
function dstrpos($string, $arr, $returnvalue = false) {
if(empty($string)) return false;
foreach((array)$arr as $v) {
if(strpos($string, $v) !== false) {
$return = $returnvalue ? $v : true;
return $return;
}
}
return false;
}
if(checkrobot()){
echo '机器人爬虫';
}else{
echo '人';
}
?>
posted on 2015-12-18 14:27 liuwenbohhh 阅读(590) 评论(0) 编辑 收藏 举报
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步