PHP: detecting mobile device

HTTP_USER_AGENT

Beside that most of the responses of $_SERVER[‘HTTP_USER_AGENT’] may return, it appears that this is the most reliable way to track down a user agent with PHP. It is weird that most of the clients, i.e. Safari and Chrome will return something with Mozilla in it’s strings, but however it’s enough to track the “chrome” or “safari” sub strings.

All the examples bellow are from Mac OS X:

Firefox 3.6:

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6

Note: there are both Mozilla and Firefox sub strings!

Safari 4:

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; bg-bg) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10

Chrome:

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/532.9 (KHTML, like Gecko) Chrome/5.0.307.11 Safari/532.9

Note: Here they are Mozilla, Chorme and Safari!!!

Opera:

Opera/9.80 (Macintosh; Intel Mac OS X; U; en) Presto/2.2.15 Version/10.10

The day of mobile

Nowadays it’s normal to make a site with the presumption it will be visible from mobile. The war between Nexus One from Google and iPhone from Apple is just beginning and with all those devices with wide screens everything’s becoming more complicated.

User agent strings from Nexus One and iPhone

Both are weird, but both contain the keyword – “mobile” and that may help you make a check with something like this PHP snippet:

<?php
$mobile = !!(FALSE !== strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'mobile'));
?>

4 thoughts on “PHP: detecting mobile device

  1. Actually get_browser() doesn’t work on the fly. As the PHP docs says you have to have browscap.ini file setup. And if not only the old school HTTP_USER_AGENT remains working!

    greetings,
    stoimen

  2. Great and helpful tutorial. Thanks for sharing this. I am using this function to detect mobile device for my website.

    function mobile(){
    if(stristr($_SERVER[‘HTTP_USER_AGENT’],’windows’)&&!stristr($_SERVER[‘HTTP_USER_AGENT’],’windows ce’)){
    return false;
    }
    if(eregi(‘up.browser|up.link|windows ce|iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|pda|psp’,$_SERVER[‘HTTP_USER_AGENT’])){
    return true;
    }
    if(stristr($_SERVER[‘HTTP_ACCEPT’],’text/vnd.wap.wml’)||stristr($_SERVER[‘HTTP_ACCEPT’],’application/vnd.wap.xhtml+xml’)){
    return true;
    }
    if(isset($_SERVER[‘HTTP_X_WAP_PROFILE’])||isset($_SERVER[‘HTTP_PROFILE’])||isset($_SERVER[‘X-OperaMini-Features’])||isset($_SERVER[‘UA-pixels’])){
    return true;
    }
    $a=array(‘acs-‘,’alav’,’alca’,’amoi’,’audi’,’aste’,’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’,’opwv’,’palm’,’pana’,’pant’,’pdxg’,’phil’,’play’,’pluc’,’port’,’prox’,’qtek’,’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’,’w3c ‘,’wap-‘,’wapa’,’wapi’,’wapp’,’wapr’,’webc’,’winw’,’winw’,’xda’,’xda-‘);
    if(isset($a[substr($_SERVER[‘HTTP_USER_AGENT’],0,4)])){
    return true;
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *