Welcome to WebmasterWorld Guest from 3.81.29.226

Forum Moderators: phranque

Message Too Old, No Replies

Browser Sniffing

when all else fails...

     
12:57 am on Jun 15, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month

joined:Mar 15, 2013
posts: 1205
votes: 119


Can you guys suggest how I might test if a browser matches any of these? I know browser sniffing is generally frowned upon, but I don't think I have a choice :-/

IE 5.5+
IE Mobile 10+
Edge 12+
Firefox 3.5+
Chrome 4+
Safari 3.1+
Opera 9+
Opera Mobile 12.1+
iOS Safari: 5.0+
Samsung 4+
Android 3+
Android Chrome 66
Android Firefox 60

// No clue what these are, but they're on the list
Android UC 11.8
Android QQ 1.2
BB 7+
Baidu 7.12


I can test within PHP, JavaScript, or jQuery. The only way I've found that seems to be consistently accurate is PHP's get_browser, but I've read that it's pretty slow using PHP 5.6 (my current version). And there's the minor issue that I have no idea how some of these will actually be listed...?

$compatible = false;
$browser = get_browser(null, true);

if (
($browser['browser'] == 'MSIE' && $browser['version'] >= 5.5) ||
($browser['browser'] == 'Firefox' && $browser['version'] >= 3.5) ||
...
) $compatible = true;


I think I can get around the speed issue by putting it in an Ajax script and setting a cookie, but figuring out the user agent for each browser to know what to look for... that's a challenge!
2:37 am on June 15, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15928
votes: 884


Watch out for Safari/Chrome/Opera (versions after 12), as their UA strings overlap. See if you can check for Opera first-- if not Opera then check for Chrome-- if not Chrome then check for Safari. (That's for desktops. Mobiles have even more permutations.)

I would do it in two or more steps: first get a browser family (FF, MSIE and so on) and then check version numbers within the family you’ve arrived at. Seems like a Select/Case/Switch structure (terminology depends obviously on what language you end up in) would fit better than a series of IFs, though I realize this may have more to do with individual coding style. The idea is that if, say, they're using MSIE 4 (hahaha), then the compatibility test would fail--but there's no point in continuing to check for other options, because everything else will end up failing too.

As an alternative, is there any possibility of throwing in a quick script to test for whatever specific functionality you need, independent of browser version?
3:11 am on June 15, 2018 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month

joined:Mar 15, 2013
posts: 1205
votes: 119


As an alternative, is there any possibility of throwing in a quick script to test for whatever specific functionality you need, independent of browser version?

Maybe... I have another thread specifically about testing for contenteditable and I don't want to overlap the topics, so I'll answer you on that thread.