PHP判断当前使用的是什么浏览器(推荐)

所属分类: 网络编程 / PHP编程 阅读数: 811
收藏 0 赞 0 分享

PHP简单判断当前使用的是什么浏览器,判断浏览器类型的方法,方便统计网站访问用户使用浏览器的比例。

判断浏览器类型方法一

function userBrowser() { 
 $user_OSagent = $_SERVER['HTTP_USER_AGENT']; 
 
 if (strpos($user_OSagent, "Maxthon") && strpos($user_OSagent, "MSIE")) { 
 $visitor_browser = "Maxthon(Microsoft IE)"; 
 } elseif (strpos($user_OSagent, "Maxthon 2.0")) { 
 $visitor_browser = "Maxthon 2.0"; 
 } elseif (strpos($user_OSagent, "Maxthon")) { 
 $visitor_browser = "Maxthon"; 
 } elseif (strpos($user_OSagent, "MSIE 9.0")) { 
 $visitor_browser = "MSIE 9.0"; 
 } elseif (strpos($user_OSagent, "MSIE 8.0")) { 
 $visitor_browser = "MSIE 8.0"; 
 } elseif (strpos($user_OSagent, "MSIE 7.0")) { 
 $visitor_browser = "MSIE 7.0"; 
 } elseif (strpos($user_OSagent, "MSIE 6.0")) { 
 $visitor_browser = "MSIE 6.0"; 
 } elseif (strpos($user_OSagent, "MSIE 5.5")) { 
 $visitor_browser = "MSIE 5.5"; 
 } elseif (strpos($user_OSagent, "MSIE 5.0")) { 
 $visitor_browser = "MSIE 5.0"; 
 } elseif (strpos($user_OSagent, "MSIE 4.01")) { 
 $visitor_browser = "MSIE 4.01"; 
 } elseif (strpos($user_OSagent, "MSIE")) { 
 $visitor_browser = "MSIE 较高版本"; 
 } elseif (strpos($user_OSagent, "NetCaptor")) { 
 $visitor_browser = "NetCaptor"; 
 } elseif (strpos($user_OSagent, "Netscape")) { 
 $visitor_browser = "Netscape"; 
 } elseif (strpos($user_OSagent, "Chrome")) { 
 $visitor_browser = "Chrome"; 
 } elseif (strpos($user_OSagent, "Lynx")) { 
 $visitor_browser = "Lynx"; 
 } elseif (strpos($user_OSagent, "Opera")) { 
 $visitor_browser = "Opera"; 
 } elseif (strpos($user_OSagent, "Konqueror")) { 
 $visitor_browser = "Konqueror"; 
 } elseif (strpos($user_OSagent, "Mozilla/5.0")) { 
 $visitor_browser = "Mozilla"; 
 } elseif (strpos($user_OSagent, "Firefox")) { 
 $visitor_browser = "Firefox"; 
 } elseif (strpos($user_OSagent, "U")) { 
 $visitor_browser = "Firefox"; 
 } else { 
 $visitor_browser = "其它"; 
 } 
 return $visitor_browser; 
}

方法二

下面这个是php通过正则匹配的,理论上效率不如上面的方法一, 大家可以根据需要使用。

private function getBrowser(){
		$flag=$_SERVER['HTTP_USER_AGENT'];
		$para=array();
		
		// 检查操作系统
		if(preg_match('/Windows[\d\. \w]*/',$flag, $match)) $para['os']=$match[0];
		
		if(preg_match('/Chrome\/[\d\.\w]*/',$flag, $match)){
			// 检查Chrome
			$para['browser']=$match[0];
		}elseif(preg_match('/Safari\/[\d\.\w]*/',$flag, $match)){
			// 检查Safari
			$para['browser']=$match[0];
		}elseif(preg_match('/MSIE [\d\.\w]*/',$flag, $match)){
			// IE
			$para['browser']=$match[0];
		}elseif(preg_match('/Opera\/[\d\.\w]*/',$flag, $match)){
			// opera
			$para['browser']=$match[0];
		}elseif(preg_match('/Firefox\/[\d\.\w]*/',$flag, $match)){
			// Firefox
			$para['browser']=$match[0];
		}elseif(preg_match('/OmniWeb\/(v*)([^\s|;]+)/i',$flag, $match)){
			//OmniWeb
			$para['browser']=$match[2];
		}elseif(preg_match('/Netscape([\d]*)\/([^\s]+)/i',$flag, $match)){
			//Netscape
			$para['browser']=$match[2];
		}elseif(preg_match('/Lynx\/([^\s]+)/i',$flag, $match)){
			//Lynx
			$para['browser']=$match[1];
		}elseif(preg_match('/360SE/i',$flag, $match)){
			//360SE
			$para['browser']='360安全浏览器';
		}elseif(preg_match('/SE 2.x/i',$flag, $match)) {
			//搜狗
			$para['browser']='搜狗浏览器';
		}else{
			$para['browser']='unkown';
		}
		return $para;
	}

保存$para即可

php判断浏览器是不是IE

1、$_SERVER['HTTP_USER_AGENT']和strpos

2、打印结果

谷歌:
 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36"
 
火狐:
 "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0"
 
IE:
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"  

3、php控制器中

网上说用判断ua中是否有MSIE,然而并没有,用Triden判断也可以的。

public function isIE() {
  $isIE = strpos($_SERVER['HTTP_USER_AGENT'],"Triden");
  return $isIE; 
}

总结

以上所述是小编给大家介绍的PHP判断当前使用的是什么浏览器(推荐),希望对大家有所帮助!

更多精彩内容其他人还在看

TP5(thinkPHP5)框架基于ajax与后台数据交互操作简单示例

这篇文章主要介绍了TP5(thinkPHP5)框架基于ajax与后台数据交互操作,结合实例形式分析了thinkPHP5前端基于jQuery的ajax数据提交及后台数据接收、处理相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP利用Mysql锁解决高并发的方法

这篇文章主要介绍了PHP利用Mysql锁解决高并发的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

php 后端实现JWT认证方法示例

这篇文章主要介绍了php 后端实现JWT认证方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

ThinkPHP框架实现定时执行任务的两种方法分析

这篇文章主要介绍了ThinkPHP框架实现定时执行任务的两种方法,结合实例形式分析了2种被动执行定时任务的相关操作技巧与注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP命名空间与自动加载类详解

这篇文章主要介绍了PHP命名空间与自动加载类,结合实例形式详细分析了php自动加载类与命名空间原理、使用方法及相关操作注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP时间处理类操作示例

这篇文章主要介绍了PHP时间处理类,结合实例形式分析了DateTime、DateTimeZone、DateInterval及DatePeriod等常用日期时间处理类简单操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

利用PHP扩展Xhprof分析项目性能实践教程

XHProf是Facebook开发的性能调试工具,能帮助直观的统计显示PHP程序执行中各方法函数调用次数和消耗时间,以方便我们排查性能瓶颈并进行调优。下面这篇文章主要给大家介绍了关于利用PHP扩展Xhprof分析项目性能实践的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Django 标签筛选的实现代码(一对多、多对多)

这篇文章主要介绍了Django 标签筛选的实现代码(一对多、多对多),本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

PHP使用pdo实现事务处理操作示例

这篇文章主要介绍了PHP使用pdo实现事务处理操作,结合实例形式较为详细的分析了php基于pdo实现事务处理的相关原理与操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

thinkPHP框架实现类似java过滤器的简单方法示例

这篇文章主要介绍了thinkPHP框架实现类似java过滤器的简单方法,结合实例形式分析了thinkPHP基于继承实现的登录验证功能相关操作方法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多