鹿泽鹿泽  2024-07-20 10:59:24 鹿泽笔记 隐藏边栏  0 

使用php脚本来判断客户端浏览器有类型以及版本语言的方法:

php获取用户浏览器的类型

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;
}

调用方法:

echo userBrowser();

php获取访客的浏览名称以及版本号的方法

php示例代码:

/**
 * 判断浏览器名称和版本
 */
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(版本号)
// Firefox(87.0)
1、文章版权归作者所有,未经允许请勿转载。
2、本站所有文章,如无特殊说明或标注,均为本站原创发布。任何在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们邮箱:526009505@qq.com进行处理。
3、咨询请联系QQ:526009505