php判断访客浏览器类型及版本号的方法
墨初 编程开发 2922阅读
访客访问网站时,传入的UserAgent信息中就携带访客的浏览器类型及版本号,我们只需要使用php脚本去分析判断一下UserAgent信息的相关信息,就可以获取到访客的浏览器类型及版本号。
php获取用户浏览器的类型
php示例代码:
/** * 获取用户客户端浏览器的类型 * http://73so.com * @param null * * @return string */ 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; }
调用方法:
echo userBrowser();
php 获取访客的浏览名称以及版本号的方法
php示例代码:
/** * 判断浏览器名称和版本 * http://73so.com * @param null * * @return string */ function get_broswer() { $ua = $_SERVER['HTTP_USER_AGENT']; if (stripos($ua, "Firefox/") > 0) { preg_match("/Firefox\/([^;)]+)+/i", $ua, $b); $exp[0] = "Firefox"; $exp[1] = $b[1]; //获取浏览器的版本号 } elseif (stripos($ua, "Maxthon") > 0) { preg_match("/Maxthon\/([\d\.]+)/", $ua, $aoyou); $exp[0] = "傲游"; $exp[1] = $aoyou[1]; } elseif (stripos($ua, "MSIE") > 0) { preg_match("/MSIE\s+([^;)]+)+/i", $ua, $ie); $exp[0] = "IE"; $exp[1] = $ie[1]; } elseif (stripos($ua, "OPR") > 0) { preg_match("/OPR\/([\d\.]+)/", $ua, $opera); $exp[0] = "Opera"; $exp[1] = $opera[1]; } elseif(stripos($ua, "Edge") > 0) { //注意 Edge浏览器添加了 chrome 内核标记 需要在判断Chrome浏览器之前进行匹配 preg_match("/Edge\/([\d\.]+)/", $ua, $Edge); $exp[0] = "Edge"; $exp[1] = $Edge[1]; } elseif (stripos($ua, "Chrome") > 0) { preg_match("/Chrome\/([\d\.]+)/", $ua, $google); $exp[0] = "Chrome"; $exp[1] = $google[1]; } elseif (stripos($ua,'rv:')>0 && stripos($ua,'Gecko')>0){ preg_match("/rv:([\d\.]+)/", $ua, $IE); $exp[0] = "IE"; $exp[1] = $IE[1]; } elseif (strpos($ua, 'MicroMessenger') !== false ){ preg_match('/MicroMessenger\/([\d\.]+)/i',$ua,$match); $exp[0] = "微信浏览器"; $exp[1] = $match[1]; } else { $exp[0] = "未知浏览器"; $exp[1] = ""; } return $exp[0].'('.$exp[1].')'; }
调用方法:
echo get_broswer(); // Chrome(91.0.4472.164) // Firefox(87.0)