Detecting mobile devices?

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
ridelikeaturtle
Forum Members
Forum Members
Posts: 103
Joined: Thu May 06, 2010 9:22 pm

Detecting mobile devices?

Post 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.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Detecting mobile devices?

Post 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.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
nockenfell
Power Poster
Power Poster
Posts: 751
Joined: Fri Sep 12, 2008 2:34 pm
Location: Schweiz / Switzerland

Re: Detecting mobile devices?

Post by nockenfell »

That could be interesting for you:
http://code.google.com/p/php-mobile-detect/
applejack
Power Poster
Power Poster
Posts: 1015
Joined: Fri Mar 30, 2007 2:28 am
Location: London

Re: Detecting mobile devices?

Post 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.
Post Reply

Return to “Modules/Add-Ons”