Finally a browser detector you can depend on. Let's face it, with all of the browsers on the market today, you need a slightly more sophisticated tool than just searching for 'MSIE' in the user agent, right?
There are browsers that spoof as other browsers so they get accepted at more sites (Opera spoofing as IE); there are browsers built out of other browsers (AOL browser).
Browser detection is not for the faint of heart. That's why you should be using the earthli Browser Detector. We've done all of the leg work for you and come up with a solid approach to picking apart a user agent and getting at the facts.
All browser detectors have to work with the user agent string. User agents can happily
lie about who they are. A liar will fool any browser detector, including this one. Most
browsers don't lie, and even those that spoof can't help writing their name in the user
agent string somewhere.
Where this browser detector parts ways with most is that it clearly separates its tasks:
The detector will reliably figure out which browser and operating system is being used, then consults a table of capabilities to let you know if a particular operation should be supported. The algorithm, described below, should be quite forward-compatible (and has indeed proved to be with subsequent browser releases) and only needs to be updated when:
The detector is table-driven, so even these types of changes were handled in 'data', without changing the algorithm at all.
After careful analysis of an enormous list of available user agents, a few similarities cropped up:
The earthli Browser Detector then defines a few tables from the information above:
Armed with these, the algorithm reads from left to right, pulling out name/version pairs and executing the following logic.
system_name
and interpreted_system_name. The first is the version info directly extracted from the
string; the second is a user-friendly version recognizable to the user. For example, if Windows NT 5.0
is specified in the user agent, interpreted_system_name will return Windows 2000.The Phoenix browser (I know, I know, but that's what it was called when I did my tests) provides a preference to change the user agent transmitted in the HTTP header. This was used along with aforementioned list of user agents to spoof as many different browsers in testing.
All of the cool properties and values discovered during analysis are best used only for
displaying or recording information about a browser. Use the supports function
to find out whether a browser can or can't do something, like Alpha-PNGs, CSS2 or DHTML.
If you need to check a property that isn't covered by supports and its flags, you'll have to
use the is and the various name and version functions to do the test yourself.
There is full documentation for every function and property you need. You
can look up how supports uses is and version numbers.
If you do come up with a useful check, let me know and we'll build it into supports directly.
Or, better yet, do it yourself and mail it to me. I'll post an update with your changes in it.
The implementation is PHP only, but the algorithm is quite language-independent and can easily be ported to any other language with regular expressions. At some point, there should be a JavaScript version for those that would rather know all this stuff client-side. If you're in a hurry, try porting it yourself ... it's relatively well-documented and should port pretty much one-to-one.