One problem that keeps cropping up when developing mobile content is how to differentiate between mobile devices and desktop browsers. The "proper" way to do this is to use a full device database such as WURFL (and we have an article on how to do this) 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.
But the solution is there. 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 (dev.mobi profile) and you can find the original here. The algorithm used is fairly lightwight -- 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.
$mobile_browser = '0';
if(preg_match('/(up.browserup.linkmmpsymbiansmartphonemidpwapphone)/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:
Adding the W3C UA string (Default Delivery Context)
Specal case detection for Opera Mini
Catch-all exception for devices with Windows in the UA string (Opera 9 for Windows was being recognised as a mobile device)
Known problem is with the windows smart phone.. the tiny internet explorer.
Which send an agent like this:
HTC_P3300 Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 7.6)
This example shows the default Agent of a HTC P3300 Smartphone with Windows Mobile 6.As there is a "Windows" in the text, the code above detects a desktop-system.
So let us have improvement code.
Saturday, November 24, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment