简体   繁体   English

检查浏览器 HTML5 与 PHP 的兼容性

[英]Check Browser HTML5 Compatibility with PHP

I want to use input types or other features from HTML5.我想使用 HTML5 中的输入类型或其他功能。 But not all browsers support these HTML5 features.但并非所有浏览器都支持这些 HTML5 功能。

How can I check if a user's browser is html5 compatible with PHP code only ?.如何检查用户的浏览器是否PHP 代码兼容 html5?

By the way, I don't like modernizr .顺便说一句,我不喜欢Modernizr I have to figure out with PHP.我必须弄清楚PHP。

Example:例子:

Html5 compatible browser: Html5 兼容浏览器:

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

Html5 incompatible browser: Html5 不兼容的浏览器:

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

PHP is really the wrong place to be doing this kind of detection. PHP确实是进行这种检测的错误场所。 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.成功处理浏览器兼容性的关键是检测对您需要的功能的支持,并使用 polyfill 或优雅地降级这些特定功能。 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:对于特征检测,Modernizr 是一个很棒的小工具,但如果你真的不喜欢它,就像你在问题中所说的那样,这里有一个很小的 ​​JS 函数,专门检测对日期输入字段类型的支持:

/**
 * @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:这是你的 HTML:

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

Now you can have a tiny bit of jQuery that does the following:现在,您可以使用一小部分 jQuery 来执行以下操作:

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

Simple as that.就那么简单。

Of course, Modernizr would make it better.当然,Modernizr 会让它变得更好。 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. Modernizr 本身不执行 polyfill,因此如果不支持日期输入类型,您仍然需要类似于上述的代码来应用 datepicker 插件,但是 Modernizr 所做的我在上面的代码中没有做的是允许您告诉浏览器仅在需要时才加载日期选择器库。 This would save a lot of bandwidth for modern browsers.这将为现代浏览器节省大量带宽。 Modernizr makes this kind of thing dead easy. Modernizr 使这种事情变得非常容易。

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.如果你觉得你必须用 PHP 来做,那就这样吧,但要注意你违背了几乎所有专家的建议,从长远来看,这会让你更头疼。

The better (and established) best practice is to更好(且已确立)的最佳实践是

a) make everything work with non-html5 browsers too a) 使一切也适用于非 html5 浏览器

OR或者

b.) simply make your app HTML5-only, and don't give old browsers the possibility to use it b.) 只需让您的应用程序仅支持 HTML5,并且不要让旧浏览器可以使用它

OR或者

c) use a JS tool that emulates (some) HTML5 features in older browsers: http://en.wikipedia.org/wiki/HTML5_Shiv ! c) 使用在旧浏览器中模拟(某些)HTML5 功能的 JS 工具: 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.无法仅使用 PHP 进行功能检测。

I'd re think you're dislike for Modernizr it's really quite awesome.我认为你不喜欢 Modernizr,它真的很棒。

Below class may be help you : Not 100% solution but you can go ahead.下面的课程可能对您有所帮助:不是 100% 的解决方案,但您可以继续。 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 类。一旦检测到浏览器,您就可以根据您的要求设置条件进行测试。

 <?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] =>

) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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