简体   繁体   中英

How can I detect browser features?

Is it possible to write a script in PHP which would detect browser HTML5 and CSS3 feature support? If not, is it possible in any language?

PHP doesn't know much about the client, past what is supplied via HTTP and stored in the $_SERVER superglobal. This is pretty much by design, since PHP runs on the server and not on the client.

Feature detection more aptly belongs in client code, aka JavaScript. Modernizr is one of the best libraries available that offers easy feature detection.

Using this, you can detect features on the client and then issue an AJAX request that will update variables on the server. It won't work for the first page load, but every request after that will be able to rely on those settings being available for interrogation on the server if you store them in Session.

A solution for a workaround to this would be to first see if values are stored on the server, if not to output a page that simply performs this feature detection/AJAX call and then redirects back to whatever page was initially requested via javascript. There are some pitfalls to this solution (all visitors to your site will initially see a blank screen with a quick redirect which is not the best way to build trust, plus you're potentially setting yourself up for some nasty situations that you may never find out about because your server code is now dependent on client implementations and I doubt you will test your application on every single client available). I only offer this as a workaround.

您是否看过http://www.html5test.com并了解其工作原理。

PHP has a function called get_browser to return an associative array of browser features. However, you need to download a file called browscap.ini that contains information on browsers so the function will work properly. Instructions on how to get and install this file can be found on the documentation page for the function.

You should detect the browser in php then use different code for different browser.

PHP browser detection techniques:

  1. google plugin for mobile detection
  2. Using $_SERVER['HTTP_USER_AGENT']
  3. Using get_browser()
  4. By Mobiforge

Something that I've found very helpful is to serialize modernizr to a json string and post it to the server via ajax. On the server, persist this to your session and you can now send customized content based upon the features available to your visitor.

var result = JSON.stringify(Modernizr);
$.post('modern.php',{json: result});

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