Articles | Design & Development | Research, Stats & Analysis | News & Comment
HTML5 | Android | iPhone | Wearables | Touch | Content Adaptation | Geolocation | Emulators | Messaging | Device Detection | Responsive Design | More >

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.

Update: We have an improved version of the algorithm which includes tablet detection here.

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.

<?php
 
$mobile_browser = '0';
 
if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
    $mobile_browser++;
}
 
if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
    $mobile_browser++;
}    
 
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
$mobile_agents = array(
    'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
    'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
    'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
    'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
    'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
    'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
    'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
    'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
    'wapr','webc','winw','winw','xda ','xda-');
 
if (in_array($mobile_ua,$mobile_agents)) {
    $mobile_browser++;
}
 
if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini') > 0) {
    $mobile_browser++;
}
 
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) {
    $mobile_browser = 0;
}
 
if ($mobile_browser > 0) {
   // do something
}
else {
   // do something else
}   
 
?>

The changes changes made from Andy's original version are:

If anyone has any improvements on this code, or implementations for other languages, please let us know!

See also an ASP version.

Posted by ronan - 01 Jun 2007


Related content...

Back to top
Switch to our desktop site | Archive