Lightweight Device-Detection in PHP

One problem that keeps cropping up when developing mobile content is how to differentiate between mobile devices and desktop browsers. We need to be able to do this before we can think about content adaptation. The "proper" way to do this is to use a full device detection database such as DeviceAtlas (see our tutorial here) but the following method may be sufficient to get you started.

This PHP code implements a mobile device detection algorithm that works for a good percentage of mobile browsers out there. This code is the work of Andy Moore. The algorithm used is fairly lightweight—the code is mostly based on a list of about 90 well-known mobile browser User-Agent string snippets, with a couple of special cases for Opera Mini, the W3C Default Delivery Context and some other Windows browsers. The code also looks to see if the browser advertises WAP capabilities as a hint.

In our experience, this code does a fairly good job. It could probably be improved but it's certainly not a bad start, and is lightweight enough not to cause major problems. For more accurate detection and increased performance we recommend using a dedicated device detection such as DeviceAtlas. Doing so will result in much higher performance, reduced load on your server and much better accuracy.

45 Comments

I’ve got this code running and find everything fine down to the line which says
if($mobile_browser>0)
do something

What I’d like to do at that point is redirect this link to another page. I’m told from other articles that the solution is to use “header (Location:”, but when I do so, I’m told “Cannot modify header information – headers already sent”.

There’s nothing in my php script except the php code (line 1 is the “< ?php" so the classic example of inserting HTML at the beginning hasn't been done. Any thoughts about why headers have already been sent? I would like suggestions about how to "do something" that can stay on the server. If I can't, can you supply code that would set an HTML or Javascript variable so just a few lines on the client would do the redirect?
You can see my full code at http://www.lighthousesrus.org/mobile/test.php

Thanks for this code ! After some tests we identified a problem with Macintosh browsers such like Opera.
For example, the UserAgent of a typical Opera installation on Mac OS is : “Opera/9.63 (Macintosh : PPC Mac OS X : U : fr) Presto / 2.1.1”

In this case, the code identifies the browser like a mobile_browser which is not the case. It comes from the $mobile_agents array which contains the string “oper”. So each Opera browser on Macintosh is seen like a mobile browser (not the on Windows because of the ‘windows’ test).

Any idea on a patch to avoid that (deleting “oper” string doesn’t look like the best solution) ?
Thanks in advance,
Jmanj

I’ve been reading recently some posts, and maybe a little explanation ’bout basic app architecture could bring some light – where to go, and how to take advantage of the “location” function.
This post has a twofold goal:
* Show an explicit URL to go (wether mobile or not)
* Overcome the infamous “Cannot modify header information – headers already sent” error message 😉

/*
Now, it’s **VERY IMPORTANT AGAIN**
This allows headers & content to be sent to the browser
*/
[b]ob_end_flush();[/b]
?>

[/code]

Really, this example could grow a bit more, to become something called “page controller” (look in Google or Wikipedia for more on programming patterns): you have an almost blank “index” page, and depending on some conditions, you execute or call (include or require) one or another “code chunk”.
Of course, this is just a skeleton – you should taylor it to your specific needs – but while being basic, it works.
Hope you find it useful.

This script will [b]always[/b] redirect mobile devices to the mobile page – depending on how well you’ve designed your mobile site and whether you’ve hidden functionality or not – this may not always be what the user wants.

Play it safe and allow the user a way to force either the full or mobile versions of the site – you can do this easily with an extra GET parameter on requests:

In your PHP always check for the parameters before you do the User-Agent happy dance:
[code]
< ?php
if ($_GET['mobile']) {
$is_mobile = true;
}
if ($_GET['full']) {
$is_mobile = false;
}
[/code]
Cheers,
John
P.S
Dotmobi dudes, this input box is super small - we're not typing a tweet here - this is for a post!
[b]Update[/b] in my habit of reading the oldest posts first I missed James’ post on doing what I described above.

hey guys, i am currently building a buddy of mine a web site for his business, and I am trying to create a mobile site (as his index is a flash intro, and cannot be seen by most mobile devices.) I am extremely new to the mobile device web design, and am not too experienced with php or asp. Can you give me a step by step to introducing this redirect on my site? thank you very much and greatly appreciated.

Hi,
you can use Apache Mobile Filter and the keep the information of capability as environment variable, I think it’s a good solution for work with any language supported with Apache Web Server (Ruby, PHP, JSP, perl).

My php script except the php code (line 1 is the “< ?php" so the classic example of inserting HTML at the beginning hasn't been done. Any thoughts about why headers have already been sent? I would like suggestions about how to "do something" that can stay on the server

I am new in making mobile based website. My question is :
[i]My website is fully static and made in simple html pages. I have made the mobile version and make its subdomain “www.m.abc.com”
Now is there any script which can redirect the user when he/she open my website on mobile and redirect he/she when seeing my website on desktop

This is a website of Afilias Technologies Ltd, a private company limited by shares, incorporated and registered in the Republic of Ireland with registered number 398040 and registered office at 6th Floor, 2 Grand Canal Square, Dublin 2, Ireland