PHP检测浏览器类型的背景
在PHP开发中,有时需要识别访问者的浏览器类型,以便进行数据统计或兼容性处理。通过获取客户端的User-Agent信息,可以简便地判断出用户正在使用的浏览器种类。

第一种判断浏览器类型的方法通常基于简单的字符串查找。
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;
}
第二种方法同样用于识别浏览器类型。
接下来展示的是利用正则表达式进行匹配的方案。从理论效率上看,该方法可能不如前述的strpos方式高效,开发者可根据实际需求选择合适的方式。
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中即可。
此外,还需要了解如何专门判断浏览器是否为Internet Explorer (IE)。
常用的手段是结合$_SERVER['HTTP_USER_AGENT']全局变量与strpos函数进行子串查找。
以下是不同浏览器生成的User-Agent字符串示例,用于对比分析。
谷歌浏览器: "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"
在PHP控制器中调用上述检测逻辑。
网上有观点认为应检测UA中是否包含MSIE,但实际上新版IE已不再包含该标识,转而使用Trident内核标识进行判断更为准确。
public function isIE() {
$isIE = strpos($_SERVER['HTTP_USER_AGENT'],"Triden");
return $isIE;
}
总结
以上所述是小编给大家介绍的PHP判断当前使用的是什么浏览器(推荐),希望对大家有所帮助!