您好!欢迎来到深圳市互联网学会官网!
首页 > 电脑技术 > IT编程 > php判断搜索引擎蜘蛛爬虫的代码

php判断搜索引擎蜘蛛爬虫的代码

  • A-
  • A+
    2017-11-10
  • 分享

php判断搜索引擎蜘蛛爬虫还是人为访问代码,摘自Discuz x3.2

<?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 '人';
}
?>

php判断搜索引擎蜘蛛爬虫还是人为访问代码,摘自Discuz x2_Beta

<?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 '人';
}
?>

  • 分享
关键词:
(编辑:丨本文链接:https://www.isz.org.cn/computer/1/18.html

深圳市互联网学会

学会的宗旨:整合行业资源,服务互联网,普及互联网知识,打造互联网在线学习、互联网生态服务。
更多学习进入互联网在线学习《互联网学会》公众号

欢迎加入学会
我们随时为您提供帮助

成为会员
相关推荐 查看更多

热门文章

查看更多

咨询热线

0755-32875048

周一至周五 9:00-18:00

地址:深圳市福田区南园路68号上步大厦18A 特别声明: 本会所有证书与官网一致为准。

  • 深圳市互联网学会官方微博扫码加客服微信
  • 深圳市互联网学会官方微信关注微信公众号
@深圳市互联网学会 版权所有
  备案号:粤ICP备14018046号