How do I make this faster? Not that it's slow but I want to make it quicker!
This little bit of code sniffs the user agent and accept headers of the browser hitting it to determine if it's a mobile device or not. Then you can either redirect mobile requests to a mobile friendly site or render a mobile specific version to them.
This is running on the dev.mobi site for mobile development, over 1,300 WordPress site and it's used on a popular mobile site builder which is powering over 5,000 sites.
I know the mobile device detection works and is sound but I wondered how some of the folks here would hack it to make it slicker and more efficient.
// mobile device detection starts -
$mobile_browser = '0';
// sniff for any mobile specific strings in the user-agent value
if(preg_match('/(up.browser|up.link|windows ce|iemobile|mmp|symbian|smartphone|midp|wap|phone|vodafone|o2|pocket|mobile|pda|psp)/i',strtolower($_SERVER['HTTP_USER_AGENT']))){
$mobile_browser++;
}
// sniff for mobile specific accepts and headers
if(((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'text/vnd.wap.wml')>0) or (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0)) or ((((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or isset($_SERVER['X-OperaMini-Features']) or isset($_SERVER['UA-pixels'])))))){
$mobile_browser++;
}
// build an array of the first four characters from a list of common mobile user-agents
$mobile_agents = 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','wap-','wapa','wapi','wapp','wapr','webc','winw','winw','xda','xda-');
// check if there's a match
if(in_array(strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4)),$mobile_agents)){
$mobile_browser++;
}
if($mobile_browser>0){
// redirect or show mobile version of site
}