今天有人来询问利用php脚本来判断客户端浏览器有类型以及版本语言的方法,下面就写几个示例供大家参考。
php获取用户浏览器的类型
php示例代码:
/**
* 获取用户客户端浏览器的类型
* https://cx9.cn
* @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示例代码:
/**
* 判断浏览器名称和版本
* https://cx9.cn
* @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)
THE END
喜欢就支持一下吧