Friday 30 March 2012

How To Identify Mobile Browsers Using PHPHow To Identify Mobile Browsers Using PHP

Left QuoteThere are all sorts of overly-complicated functions to identify whether the user has a mobile browser or a desktop. This function is nice and clean, and returns either true (if mobile) or false (if not).

function mobile_browser() {
if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|pad|phone|
android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) return true;
if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0)
OR ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']))))
return true;

$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(strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4)),$mobile_agents))
return true;
else return false;
}


To call, it just use the following:

if (mobile_browser()) print "mobile";
else print "desktop";


Nice and simple. If you see anything that gets through, please let me know and I'll update the function.Right Quote

Next article: Sending $_SESSION variables with cURL (19 March 2013)

Next Websites article: Custom alert and confirm functions using jQuery and CSS (14 May 2013)

CommentsComments

Add your comments

Name

Comments

Question

0 + 2 = (this security question stops automated submissions)