Checking HTML5 Browser Compatibility with PHP

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

How to check if a custom browser with html5 is compatible with PHP code ?

By the way, I do not like modernizr . I have to find out using PHP.

Example:

HTML5 compatible browser:

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

Html5 incompatible browser:

 <input id="datepicker" type="text" value="" /> 
+4
source share
6 answers

PHP is really the wrong place for such a discovery. There are several reasons why this is a bad idea, and they are well documented in many places.

The key to successfully working with browser compatibility is finding support for the features you need and polyfill, or gracefully worsening those specific features. Finding the actual version of the browser or browser is bad practice and is likely to give you problems with both false positives and false negatives. Since the whole purpose of the exercise is to avoid glitch failures, the presence of inaccurate results makes all of this somewhat self-complete.

Modernizr is a great tool for discovering functions, but if you really disagree with this, as you say in this question, here is a tiny little JS function that specifically defines support for the type of a date input field:

 /** * @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 is really simple. (You can find it in more detail here , by the way)

With this feature, your site now becomes very easy to polyfill the date field.

Here is your HTML:

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

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

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

Just like that.

Of course, Modernizr will be better. Modernizr does not make polyfill itself, so you need a code similar to the one above to use the datepicker plugin if the date input type is not supported, but what Modernizr does, which I did not do in my code above, it allows you to tell the browser only to load datepicker libraries, if necessary. This will save a lot of bandwidth for modern browsers. Modernizr makes such things dead easy.

We hope that the above will give you some food for thought about the best way to do such things. All about best practice. If you feel that you should do this in PHP, then so be it, but just keep in mind that you go against the recommendation of almost all the experts there, and this will give you more headaches in the long run.

+9
source

Best (and established) best practice is

a) make everything work with browsers other than html5.

OR

b.) just make your HTML5-only application and don't let older browsers use it.

OR

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

+5
source

If detection is really necessary, you should take a look at http://detector.dmolsen.com .

+2
source

Unable to detect with PHP alone.

I would think that you do not like Modernizr, it is really amazing.

0
source

The below class may help you: not a 100% solution, but you can move on. The best way to discover the browser is to use the php class listed below. It has been found in the browser browser that you can put a test condition in accordance with 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 />"; ?> 
0
source

If you really want to do this yourself, there are at least two ways:

 $browser_string = $_SERVER['HTTP_USER_AGENT']; $browser = get_browser(null, true); 

Which will create 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] => 

)

0
source

Source: https://habr.com/ru/post/1493387/


All Articles