简体   繁体   中英

Check Browser HTML5 Compatibility with PHP

I want to use input types or other features from HTML5. But not all browsers support these HTML5 features.

How can I check if a user's browser is html5 compatible with PHP code only ?.

By the way, I don't like modernizr . I have to figure out with PHP.

Example:

Html5 compatible browser:

<input type="date" value="" />

Html5 incompatible browser:

<input id="datepicker" type="text" value="" />

PHP is really the wrong place to be doing this kind of detection. There are any number of reasons why it's a bad idea, and they're well documented in many places.

The key to successfully working with browser compatibility is to detect support for the features you need, and either polyfill or gracefully degrade those specific features. Detecting the actual browser or browser version is poor practice and is likely to give you problems with both false positives and false negatives. Since the whole point of the exercise is to avoid compatibility glitches, having inaccurate results makes the whole thing somewhat self defeating.

For feature detection, Modernizr is a great little tool, but if you really don't get along with it as you say in the question, here's a tiny little JS function that specifically detects support for the date input field type:

/**
 * @returns boolean - True if browser suppors 'date' input type.
 */
function browserSupportsDateInput() {
    var i = document.createElement("input");
    i.setAttribute("type", "date");
    return i.type !== "text";
}

As you can see, it really is simple stuff. (You can find it documented in more detail here , by the way)

With that function in your site, it now becomes extremely easy to polyfill the date field.

Here's your HTML:

<input type='date' name='whatever' class='datepicker'>

Now you can have a tiny bit of jQuery that does the following:

$(function() {
    if(!browserSupportsDateInput()) {
        $(".datepicker").datepicker();
    }
});

Simple as that.

Of course, Modernizr would make it better. Modernizr doesn't do the polyfill itself, so you'd still need code similar to the above to apply the datepicker plugin if the date input type wasn't supported, but what Modernizr does which I haven't done in my code above is to allow you to tell the browser to only bother loading the datepicker library if it's needed. This would save a lot of bandwidth for modern browsers. Modernizr makes this kind of thing dead easy.

Hopefully the above will give you some food for thought about the best way to do this kind of thing. It's all about best practice. If you feel you must do it in PHP, then so be it, but just be aware that you're going against the recommendation of pretty much every expert out there, and it will give you more headaches in the long run.

The better (and established) best practice is to

a) make everything work with non-html5 browsers too

OR

b.) simply make your app HTML5-only, and don't give old browsers the possibility to use it

OR

c) use a JS tool that emulates (some) HTML5 features in older browsers: http://en.wikipedia.org/wiki/HTML5_Shiv ! This is even used in some high-end-major sites, so it's not that bad...

如果确实需要检测,您应该查看http://detector.dmolsen.com

Can't feature detect with PHP only.

I'd re think you're dislike for Modernizr it's really quite awesome.

Below class may be help you : Not 100% solution but you can go ahead. The Best way to detect the browser is to use the php class given below.once browser is detected you can put condition to test as per your requirements.

 <?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 />";
        ?> 

If you really want to do it yourself there are minimum two ways:

$browser_string = $_SERVER['HTTP_USER_AGENT'];

$browser = get_browser(null, true);

Which would produce something like

// contents of $browser_string
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3
//contents of $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] =>

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM