[nycphp-talk] Browser detection
Peter Sawczynec
ps at blu-studio.com
Sun Jun 9 21:20:25 EDT 2013
It is definitely time to be using Responsive Grid (as there are just too
many platforms and device screen sizes to go custom on all of them) and then
theoretically one would not need browser detection. But if you still have a
separate mobile version of a desktop site…
I looked at all the options (WURFL, special php class libraries, etc). They
were all too heavy and much too specific. Those old tools were for a time
when tangible code breaking platform capabilities differences and browser
differences were still important.
Today - at least here in North America - with html5/webkit standards support
virtually omnipresent, I find detecting generally for desktop vs tablet vs
mobile is really all one needs to know.
So for a couple of years now I have been using a really small javascript
browser detection script to detect for mobile by checking on a trifecta of:
a) mobile OS in the user agent string
b) orientation
c) touch
In actual use it looks quite much like the snippet below. I've made many
versions of it including with cookies to save the user preference. This
particular one below allows to route iPad users directly to the desktop
site. You can customize it by adding more or less entries into the mobile OS
regular expression and/or change up the logic a little.
var agent = navigator.userAgent.toLowerCase();
// If the screen orientation is defined we are in a modern mobile OS
var mobileOS = typeof orientation != 'undefined' ? true : false;
// If touch events are defined we are in a modern touch screen OS
var touchOS = ('ontouchstart' in document.documentElement) ? true : false;
// If is mobile user agent
var isanymobile =
(/iphone|ipad|ipod|android|blackberry|symbianos|symbian|sonyericsson|samsung
|webos|wap|motor|nokia|mini|windows\sce|palm/i.test(agent));
var isiPad = /ipad/i.test(agent);
function check_mobile() {
if (mobileOS && touchOS && isanymobile && !isiPad) {
window.location = 'http://m.anysite.com/';
}
}
check_mobile();
This post on a Samsung Galaxy Tab forum has a really good example of this
type of mobile detection script in use too:
http://www.thegalaxytabforum.com/index.php?/topic/621-detecting-android-tabl
ets-with-javascript/
There is also special website called: http://detectmobilebrowsers.com/ that
gives the basic overview of how this type of mobile OS detection works.
This concept has been discussed in detail too on stackoverflow.com
Warmest regards,
Peter Sawczynec
Technology Dir.
blūstudio
941.893.0396
ps at blu-studio.com <mailto:ps at sun-code.com>
www.blu-studio.com
From: talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org] On
Behalf Of Federico Ulfo
Sent: Wednesday, May 01, 2013 3:52 PM
To: NYPHP Talk
Subject: [nycphp-talk] Browser detection
Hi all, how do you solve the problem of the browser detection?
The PHP built in function get_browser() doesn't seams to work very well,
it's slow and inaccurate, it also need a huge browscap.ini file to be loaded
and updated, and looks like isn't maintained really well.
The most accurate library I've found uses the user-agent-string.info API,
which isn't very fast, and it doesn't tell the device type, such as iPhone,
iPad, Android, etc.
Not satisfacted by the available solutions I've decided to create a library
that with dependency injection loads other parser, so it's flexible and
accurate. The library, Sail\Useragent, is open source and available on
packagist:
https://github.com/rainphp/useragent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20130609/efc0723d/attachment.html>
More information about the talk
mailing list