Page 1 of 1

Detecting mobile devices?

Posted: Wed Nov 21, 2012 3:41 pm
by ridelikeaturtle
Like many others, I too am trying to provide different layouts/views for mobile devices for my website. I've tried the methods used here:

http://www.i-do-this.com/blog/14/Make-y ... bile-ready

Unfortunately, I'm not having much success. I can see the PHP in the example function is using the HTTP_USER_AGENT value to determine what mobile device is being used.

Code: Select all

...
$user_agent = $_SERVER['HTTP_USER_AGENT'];
...
case (eregi('android',$user_agent));       
$mobile_browser = $android;
...
The value I'm seeing when I open up a site on my Android phone for the HTTP_USER_AGENT value is:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:15.0) Gecko/20100101 Firefox/15.0

I don't see "android" anywhere in that string... this leads me to believe using HTTP_USER_AGENT is a poor way to determine if my site is being displayed on Android.

Is there a better or more correct (and dependable) way to determine if your site is being displayed on a mobile device?

I'm using CMSMS 1.10.2.

Re: Detecting mobile devices?

Posted: Wed Nov 21, 2012 3:49 pm
by calguy1000
The short answer is: NO. There is no better way than the USER_AGENT string to detect mobile devices. No other information identifying the browser comes from the browser on a request to the application.

There are online databases and web services that will attempt to find a better match using the user agent string... and are somewhat more successful than a simple regular expression... but only somewhat.

The only reasonably reliable way to custom style your site for different sized screens and support capabilities is using media queries. And even this falls down somewhat if users are using third party browsers on their phones/tablets/televisions/game consoles.

Re: Detecting mobile devices?

Posted: Wed Nov 21, 2012 7:32 pm
by nockenfell
That could be interesting for you:
http://code.google.com/p/php-mobile-detect/

Re: Detecting mobile devices?

Posted: Fri Nov 23, 2012 12:44 am
by applejack
It depends on why you want to test and what you are trying to do but using USER_AGENT is becoming old hat you should be testing for capabilities. Take a look at Modernizer which makes it easy to test for touchscreen devices as well as numerous other browser capabilities.