What's the best way to detect a browser with php?

后端 未结 5 951
我在风中等你
我在风中等你 2021-01-21 08:13

Could anyone tell me the best way to detect a browser using php? IE 6 and 7 are terrible when it comes to achieving full browser compatibility with CSS so my site isn\'t going t

相关标签:
5条回答
  • 2021-01-21 08:42

    The Best way to detect the browser is to use the php class given below. i created this for my website a year ago. It works with chrome, firefox,opera and internet explorer.

     <?php 
        class Browser { 
            public static function detect() { 
                $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']); 
                if ((substr($_SERVER['HTTP_USER_AGENT'],0,6)=="Opera/") || (strpos($userAgent,'opera')) != false ){ 
                    $name = 'opera';
                } 
                elseif ((strpos($userAgent,'chrome')) != false) { 
                    $name = 'chrome'; 
                } 
                elseif ((strpos($userAgent,'safari')) != false && (strpos($userAgent,'chrome')) == false && (strpos($userAgent,'chrome')) == false){ 
                    $name = 'safari'; 
                } 
                elseif (preg_match('/msie/', $userAgent)) { 
                    $name = 'msie'; 
                } 
                elseif ((strpos($userAgent,'firefox')) != false) { 
                    $name = 'firefox'; 
                } 
                else { 
                    $name = 'unrecognized'; 
                } 
                if (preg_match('/.+(?:me|ox|it|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches) && $browser['name']=='safari' ) { 
                    $version = $matches[1]; 
                }
                if (preg_match('/.+(?:me|ox|it|on|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches) && $browser['name']!='safari' ) { 
                    $version = $matches[1]; 
                }
                else { 
                    $version = 'unknown'; 
                } 
    
                return array( 
                    'name'      => $name, 
                    'version'   => $version,
                ); 
            } 
        } 
        $browser = Browser::detect(); 
        echo 'You browser is '.$browser['name'].' version '.$browser['version']; 
        echo "<br />";
        ?> 
    
    0 讨论(0)
  • 2021-01-21 08:52

    (Silly answer alert.)

    If you're only interested in detecting IE6 and 7, you could create an intermediary page:

    <form action="wherever.php" method="post">
        <!--[if lt IE 8]>
        <input type="hidden" name="too_old" value="1" />
        <![endif]-->
    </form>
    <script>
    document.getElementsByTagName('form')[0].submit();
    </script>
    

    That would be the entire page, as is. (You could add some stuff for people who don't have JS enabled, if you wanted.)

    Then, when it goes to the next page, you can have this PHP:

    $location = empty($_GET['too_old']) ? 'good_page.wherever' : 'bad_page.whatever';
    header('Location: '.$location);
    

    inb4 "But there are lots of ways to break this."

    0 讨论(0)
  • 2021-01-21 08:57

    You can't reliably detect what the browser is. Its as simple as that.

    Browsers are capable of lying about their identity, and frequently do.

    Some proxies and security products strip the user agent data from the request, so your PHP code may just get an empty string.

    In Javascript you may have a bit more luck, though it's still problematic, but at the PHP level you can never really be certain.

    My first suggestion would be to drop support for IE6, unless your demographic is in the really stubbon minority. IE6 usage stats is down to below 2.5% in most developed countries now. That alone will get rid of a large part of your CSS problems. IE7 is still not good, but it's a clear mile better than IE6, and it is just about supportable while sticking to modern standards.

    My second suggestion is rather than trying to downgrade your site for these browsers, try to upgrade the browser.

    There are a number of very good hacks and tools that will allow you to improve CSS support in older versions of IE. I recommend you try the following:

    • CSS3Pie
    • Selectivizr
    • Modernizr
    • Dean Edwards' IE7.js

    And of course, the ubiquitous JQuery.

    0 讨论(0)
  • 2021-01-21 08:59

    You can only reliably detect the browser user-agent using Javascript. For PHP, you have to rely on the contents of the $_SERVER['HTTP_USER_AGENT'].

    0 讨论(0)
  • 2021-01-21 09:05

    The best way to detect the user's browser with PHP is to not check the user's browser with PHP.

    You've mentioned that IE lt 8 sucks for CSS (and that is something every web dev can agree on), the best way to tell the user that their browser is too old is with a conditional comment in the HTML:

    <head>
    ...
    <!--[if IE lt 8]>
      <link rel="stylesheet" type="text/css" href="ie-styles.css" />
    <![endif]-->
    </head>
    <body>
    <!--[if IE lt 8]>
      <div id="ie-only">
        <p>Please get a <a href="http://www.abetterbrowser.org/">better browser</a>.</p>
      </div>
    <![endif]-->
    ...
    </body>
    

    The code I've written shows two things you can do: first it shows you can set an IE only stylesheet, and secondly it shows you can set IE only chunks of HTML.

    I see no reason you can't allow your IE users to use your wobsite, if it doesn't work, just let them know that it's their own fault for using a crummy browser.

    This will also allow you to set a cookie for subsequent PHP calls:

    <!--[if IE lt 8]>
    <script type="text/javascript">document.cookie='isie=oh-crud-you-use-ie';</script>
    <![endif]-->
    
    0 讨论(0)
提交回复
热议问题