Papermashup

Subscribe


Tweets


"RT @bethgordon: .@Tesco Please rethink archaic 'Approved by Mums'. Pretty sure it's not just mothers who make childcare decisions. https://…"

@ashleyford 2 days ago

"Is there ever a time that @DFS doesn't have a sale on?"

@ashleyford 6 days ago

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Lightweight Mobile Browser Detection

AshleyAshley

This simple lightweight piece of code developed by Andy Moore and modified by Mobiforge allows you to redirect your users through to a mobile version of your site. The best way to do this would be to include the following code into the top of the index page as this is most likely your first point of entry. It is advised as i did in the iphone detection tutorial found here to give users the option on mobile pages to view the original site.


<?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
}   
 
?>

Hope you find it useful :)

Designer and web developer, Co-founder and Technical Director at Harkable.com. Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]papermashup.com

Comments 5
  • Avi
    Posted on

    Avi Avi

    Reply Author

    How can i make it to detecte only mobile, no tabltes?


  • Arif
    Posted on

    Arif Arif

    Reply Author

    nice article ….


  • Rocky
    Posted on

    Rocky Rocky

    Reply Author

    I agree haven seen anything like this, will be nice to make some sorta model out of it


  • Jake Rocheleau
    Posted on

    Jake Rocheleau Jake Rocheleau

    Reply Author

    I don’t mean to be a bother and comment twice on your blog in the same day, but I just wanted to say this is a very cool article. I haven’t seen any type of script out there like this written in PHP, and I can definitely implement this into some of my upcoming projects.


    • Ashley
      Posted on

      Ashley Ashley

      Reply Author

      @jake really pleased to hear you found it useful and thanks for the feedback! 😉