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 
} 

http://www.andymoore.info/php-to-detect-mobile-phones/

    Just a thought which popped into my head, not really sure if it would work

    <style>
    @media screen {
      @import css.php?device=screen
    }
    @media handheld {
      @import css.php?device=handheld
    }
    </style>
    
    // css.php
    if( !isset( $_SESSION['device'] ))
    {
      switch( $_GET['device'] )
      {
        case 'handheld':
          $_SESSION['device'] = 'handheld';
          break;
        default:
          $_SESSION['device'] = 'screen';
      }
      // Session var now used to determine how to handle request
      header( 'Location: ' . $_SERVER['SCRIPT_URL'] );
      exit;
    }
    else
    {
      require_once( 'css/' . $_SESSION['device'] . '.css' );
    }
    

    Perhaps this breaks some standards, not too sure.

    Just a thought 🙂

      Of course that won't work, you'll just get redirected to a CSS file. The header would have to send the UA somewhere more useful 🙂

        Another couple of off-the-cuff observations. You know how you always find something in the last place you look? Reason being of course that once you find it you stop looking. Doing that here might be a good thing.

        And unless

        up.browser|up.link

        is supposed to match "up2link" and "up_browser" and so on then you probably want "." there.

        isset() would be faster than strpos() would be faster than preg_match() (see the first point). Not sure where in_array() would come into that.

        You do know you're running strtolower() twice on two strings (where in one case either its use or the /i modifier is unnecessary).

          Revised script

          new version now published, i'll mod this later to take the up. issue into account.

          thanks for the feedback so far.

            Write a Reply...