【问题标题】:get_browser() in Chrome returns 'Default Browser'Chrome 中的 get_browser() 返回“默认浏览器”
【发布时间】:2012-06-09 18:34:02
【问题描述】:

我有一个 PHP 脚本,用于在向用户显示我的页面之前检测用户浏览器。

使用 get_browser() 方法,以及从 chrome 窗口返回的数组索引“array['browser']”,返回“默认浏览器”。不知道有没有人遇到过这种情况,或者类似的情况。我敢肯定是这样的。

感谢大家的帮助。

【问题讨论】:

    标签: php google-chrome browser


    【解决方案1】:
    <?php
    function getBrowser() 
    { 
        $u_agent = $_SERVER['HTTP_USER_AGENT']; 
        $bname = 'Unknown';
        $platform = 'Unknown';
        $version= "";
    
        //First get the platform?
        if (preg_match('/linux/i', $u_agent)) {
            $platform = 'linux';
        }
        elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
            $platform = 'mac';
        }
        elseif (preg_match('/windows|win32/i', $u_agent)) {
            $platform = 'windows';
        }
    
        // Next get the name of the useragent yes seperately and for good reason
        if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) 
        { 
            $bname = 'Internet Explorer'; 
            $ub = "MSIE"; 
        } 
        elseif(preg_match('/Firefox/i',$u_agent)) 
        { 
            $bname = 'Mozilla Firefox'; 
            $ub = "Firefox"; 
        } 
        elseif(preg_match('/Chrome/i',$u_agent)) 
        { 
            $bname = 'Google Chrome'; 
            $ub = "Chrome"; 
        } 
        elseif(preg_match('/Safari/i',$u_agent)) 
        { 
            $bname = 'Apple Safari'; 
            $ub = "Safari"; 
        } 
        elseif(preg_match('/Opera/i',$u_agent)) 
        { 
            $bname = 'Opera'; 
            $ub = "Opera"; 
        } 
        elseif(preg_match('/Netscape/i',$u_agent)) 
        { 
            $bname = 'Netscape'; 
            $ub = "Netscape"; 
        } 
    
        // finally get the correct version number
        $known = array('Version', $ub, 'other');
        $pattern = '#(?<browser>' . join('|', $known) .
        ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
        if (!preg_match_all($pattern, $u_agent, $matches)) {
            // we have no matching number just continue
        }
    
        // see how many we have
        $i = count($matches['browser']);
        if ($i != 1) {
            //we will have two since we are not using 'other' argument yet
            //see if version is before or after the name
            if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
                $version= $matches['version'][0];
            }
            else {
                $version= $matches['version'][1];
            }
        }
        else {
            $version= $matches['version'][0];
        }
    
        // check if we have a number
        if ($version==null || $version=="") {$version="?";}
    
        return array(
            'userAgent' => $u_agent,
            'name'      => $bname,
            'version'   => $version,
            'platform'  => $platform,
            'pattern'    => $pattern
        );
    } 
    
    // now try it
    $ua=getBrowser();
    $yourbrowser= "Your browser: " . $ua['name'] . " " . $ua['version'] . " on " .$ua['platform'] . " reports: <br >" . $ua['userAgent'];
    print_r($yourbrowser);
    ?>
    

    来源:http://www.php.net/manual/en/function.get-browser.php#101125

    【讨论】:

    • 非常感谢。我早些时候发现了这个,但没有评估它的全部功能,因此没有意识到它做了什么。让我在奔跑。谢谢。
    • 如果我使用 Internet Explorer 11 尝试此操作,我会得到以下用户代理:“Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko”没有“MSIE” " 在其中,并且无法使用上面的功能。
    【解决方案2】:

    请参阅文档中的注释:http://us3.php.net/get_browser

    为了使其工作,您在 php.ini 中的 browscap 配置设置必须指向系统上 browscap.ini 文件的正确位置。

    browscap.ini 不与 PHP 捆绑,但您可以在此处找到最新的 » php_browscap.ini 文件。

    虽然 browscap.ini 包含许多浏览器的信息,但它依赖于用户更新来保持数据库最新。文件的格式不言自明。

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 1970-01-01
      • 2023-03-28
      • 2014-11-08
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 2014-08-10
      • 2011-12-07
      相关资源
      最近更新 更多