Jules
Jules

Reputation: 14510

User agent string for "[browser version] and later"

I'm trying to avoid tons of PHP code by specifying that a remote stylesheet, containing CSS animation, should be loaded if the HTTP_USER_AGENT string corresponds to [browser version x] or later. For example, knowing that CSS animations are supported in Chrome 19 and newer, the code would detect if Chrome's version is greater than 19, and load the appropriate stylesheet.
Is this feasible at all using PHP?

Upvotes: 0

Views: 206

Answers (3)

SDC
SDC

Reputation: 14222

If you must use PHP for this, then there is a built-in function get_browser() that does what you want.

However... If you use get_browser(), be aware that it is a bad solution to your problem.

In order to user this function, you must have an up-to-date browsecap.ini file, which is basically a text file that defines the capabilities of every browser and every version ever released.

The downsides of this are obvious:

  • You have to find and install an up-to-date copy of browsecap.ini in the first place.
  • You have to update it every time a new browser is released (and new versions are coming out frequently, so this is quite a big task).
  • It might not include all features that you want to detect.
  • It will break if the user masks their user agent string.

For all these reasons, I strongly recommend not using get_browser(), or indeed any PHP-based or server-side solution.

Instead, as others have said, you should investigate using the Modernizr library. This is a Javascript library that you install on your site that does feature detection in the browser. It will never be out of date, because it looks specifically at whether the features are supported, so it doesn't care what the actual browser is.

Upvotes: 1

Philip Tenn
Philip Tenn

Reputation: 6073

You may wish to consider using a Device Description Repository (DDR). This is an application or Web Service that you can pass in an User Agent String and get a list of known capabilities for that User Agent.

The benefits to this is that you will not have to keep updating your own code as new User Agents come about.

I read about DDRs recently in Dino Esposito's MSDN article on Mobile Site Development. He has an excellent write-up on the capabilities and nature of DDRs.

I realize you are doing PHP, but connecting / querying a DDR should be language independent.

Just a thought, hope this helps.

Upvotes: 0

Sirko
Sirko

Reputation: 74036

This is one solution, but you will have to update your parser for the user agent string from time to time to include changes therein. There is no agreed standard for use agent strings, so in general parsing the user agent string is quite hard and often comes down to lookup tables.

A more future-proof approach would be to detect the support of CSS animations on the client side using, e.g., Modernizr and then load the stylesheet based on the result. That way you don't care, if some browser developer wants to change the (structure of the) user agent string in the future.

Upvotes: 2

Related Questions