Starting > Designing > Developing > Testing > Running
Home | News | Blogs | About

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 database such as DeviceAtlas (see our tutorial here) or WURFL, but this approach has its problems. One potential problem is that you may not be in a position to get WURFL installed at your hosting location. Secondly, full device detection imposes a load on your server that may be problematic.

Luckily, there are solutions to this problem that, while not as reliable as full detection, may suffice for many cases. The following PHP code implements a mobile device detection algorithm that works for a large percentage of mobile browsers out there. This code is the work of Andy Moore (mobiForge profile) and you can find the original here. The algorithm used is fairly lightweight -- the code is mostly based on a list of about 90 well-known mobile browser UA 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.

<?php
 
$mobile_browser = '0';
 
if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/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 3 years ago

Switcher iconSwitch to our desktop site | mobiThinking