【问题标题】:Trying to detect the browser version with PHP尝试使用 PHP 检测浏览器版本
【发布时间】:2014-09-14 13:40:47
【问题描述】:

我正在尝试使用 PHP 来检测正在使用的浏览器版本。到目前为止,我已经设法找出正在使用的浏览器,但没有人可以建议我需要什么版本的浏览器才能找到它

这是我到目前为止所拥有的。谢谢

    <td><input type="hidden" name="browser" value="<?php  
    $user_agent                = $_SERVER['HTTP_USER_AGENT'];

    if (preg_match('/MSIE/i', $user_agent)) { 
    echo "Internet Explorer";
    /*} else {
    echo "Not IE";
    */}

    if (preg_match('/Firefox/i', $user_agent)) { 
    echo "Firefox";
    /*} else {
    echo "Not Firefox";
    */} 

    if (preg_match('/Chrome/i', $user_agent)) { 
    echo "Google Chrome";
    } elseif (preg_match('/Safari/i', $user_agent)) {
    echo "Safari";
    }

    ?>"/></td>

【问题讨论】:

    标签: php browser version


    【解决方案1】:

    最好的方法如下 $browser = get_browser();

    请注意,这将返回一个对象而不是字符串。所以要访问浏览器信息,你可以这样做 $brw=$browser->browser; echo $brw;

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      您可以使用浏览器类检测浏览器,从Github下载它

      配置

            include(/your-path/Browser.php);
      $browser = new Browser();
              if( $browser->getBrowser() == Browser::BROWSER_IE && $browser->getVersion() >= 8 ) 
              {
                  echo "Your browser is Internet explorer version 8";                                                                                                                                    
          }
      

      同样你可以检查所有的浏览器。

      【讨论】:

      • 此库没有稳定版本,自 2 月 15 日起未更新。
      【解决方案3】:

      试试get_browser()

      $browser = get_browser(null, true);
      echo $browser['browser'];
      echo $browser['version'];
      

      更多:- http://php.net/manual/en/function.get-browser.php

      如果 get_browser 显示警告,也可以在 php.ini 中启用

      browscap ini directive not set

      How should I be setting browscap.ini file

      【讨论】:

      • 好的,我尝试使用它,我将输入更改为文本,这样我就可以看到发生了什么,并且在框中是以下消息:警告:get_browser():browscap ini指令未在 /home/trebleclick/webapps/mafanakidsupgrade/catalog/view/theme/default/template/account/register.tpl116 中设置
      【解决方案4】:

      你可以使用下面的代码找到浏览器+版本这是html和javascript

      <html>
          <head>
            <meta http-equiv="content-type" content="text/html; charset=UTF-8">
            <title> test</title>
      
            <script type='text/javascript' src='http://code.jquery.com/jquery-2.0.0.js'></script>
      
            <style type='text/css'>
      
            </style>         
      
      
          <script type='text/javascript'>//<![CDATA[ 
          $(window).load(function(){
          var _browser = {};
      
          function detectBrowser() {
              var uagent = navigator.userAgent.toLowerCase();
              $("#result").html("User agent string: <b>" + uagent + "</b>");
      
              _browser.opera = /mozilla/.test(uagent) && /applewebkit/.test(uagent) && /chrome/.test(uagent) && /safari/.test(uagent) && /opr/.test(uagent);
              _browser.safari = /applewebkit/.test(uagent) && /safari/.test(uagent) && !/chrome/.test(uagent);
              _browser.firefox = /mozilla/.test(uagent) && /firefox/.test(uagent);
              _browser.chrome = /webkit/.test(uagent) && /chrome/.test(uagent);
              _browser.msie = /msie/.test(uagent);
              _browser.version = '';
      
              for (x in _browser)
              {
                  if (_browser[x]) {
                      if (x !== "opera") {
                          _browser.version = uagent.match(new RegExp("(" + x + ")( |/)([0-9]+)"))[3];
                          $("#result").append("<br/>The browser is " + x + " " + _browser.version);
                      }
                      else {
                          _browser.version = uagent.match(new RegExp("(opr)( |/)([0-9]+)"))[3];
                          $("#result").append("<br/>The browser is " + x + " " + _browser.version);    
                      }
                      break;
                  }
              }
      
          }
      
      
      
          detectBrowser();
      
      
          });//]]>  
      
          </script>
      
      
          </head>
          <body>
            <div id="result"></div>
      
          </body>
      
      
          </html>
      

      【讨论】:

        【解决方案5】:

        您可以使用get_browser。 PHP 文档中的示例:

        $browser = get_browser(null, true);
        print_r($browser);
        

        结果:

        Array
        (
            [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$
            [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9*
            [parent] => Firefox 0.9
            [platform] => WinXP
            [browser] => Firefox
            [version] => 0.9
            [majorver] => 0
            [minorver] => 9
            [cssversion] => 2
            [frames] => 1
            [iframes] => 1
            [tables] => 1
            [cookies] => 1
            [backgroundsounds] =>
            [vbscript] =>
            [javascript] => 1
            [javaapplets] => 1
            [activexcontrols] =>
            [cdf] =>
            [aol] =>
            [beta] => 1
            [win16] =>
            [crawler] =>
            [stripper] =>
            [wap] =>
            [netclr] =>
        )
        

        【讨论】:

          猜你喜欢
          • 2011-03-14
          • 2014-12-28
          • 1970-01-01
          • 1970-01-01
          • 2016-03-21
          • 1970-01-01
          • 2011-03-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多