PHP Mobile Device Detection (iOS, Android, BlackBerry, PlayBook, WebOS)

    Current Version: 1.0
    Price: FREE

    Mobile devices and Tablets are quickly replacing desktop computers as the premier internet devices. With the various limitations and unique capabilities of many mobile devices the practice of browser and platform detection is increasing in popularity. In certain circumstances it may be necessary — especially when trying to provide a native-like experience. For a lightweight server-side php based solution look no further than the HTTP_USER_AGENT and the function stripos. The simple example below demonstrates how to detect many popular devices including the iPhone, iPod Touch, iPad, Android, Android Tablet, WebOS, BlackBerry, & RIM PlayBook. This example can be easily modified or expanded for individual applications.

    //Detect special conditions devices
    $iPod = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
    $iPhone = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    $iPad = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
    if(stripos($_SERVER['HTTP_USER_AGENT'],"Android") && stripos($_SERVER['HTTP_USER_AGENT'],"mobile")){
            $Android = true;
    }else if(stripos($_SERVER['HTTP_USER_AGENT'],"Android")){
            $Android = false;
            $AndroidTablet = true;
    }else{
            $Android = false;
            $AndroidTablet = false;
    }
    $webOS = stripos($_SERVER['HTTP_USER_AGENT'],"webOS");
    $BlackBerry = stripos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
    $RimTablet= stripos($_SERVER['HTTP_USER_AGENT'],"RIM Tablet");
    
    
    //do something with this information
    if( $iPod || $iPhone ){
            //were an iPhone/iPod touch -- do something here
    }else if($iPad){
            //were an iPad -- do something here
    }else if($Android){
            //we're an Android Phone -- do something here
    }else if($AndroidTablet){
            //we're an Android Phone -- do something here
    }else if($webOS){
            //we're a webOS device -- do something here
    }else if($BlackBerry){
            //we're a BlackBerry phone -- do something here
    }else if($RimTablet){
            //we're a RIM/BlackBerry Tablet -- do something here
    }else{
            //we're not a mobile device.
    }
    


    If you would like information on how to detect a device other than those supported above, feel free to contact me.




    Update (9-14-2011): Added BlackBerry & Playbook detection.
    Update (9-6-2011): Split Android detection to differentiate between Tablets & Phones.

    Leave a Reply

    21 Responses to Mobile Device Detection Function

    1. This is perfect, the only problem I found is with Blackberry (maybe because I tested from BB10) but I didn’t work, I aded a new var for BB10 and fixed it, in case anyone wants it, just use this:

      $BlackBerry10 = stripos($_SERVER['HTTP_USER_AGENT'],”BB10″);

      and use

      }else if($BlackBerry10){
      //we’re a BlackBerry10 phone — do something here
      }else{
      //we’re not a mobile device.
      }

    2. It works. But now a question about nokia phones and windows phones. How must i redirect this.

      Because i redirect ipad, iphone, android mobile, webos, blackberry en rim tablet.

      But i don’t see how i must redirect windows smartphone or other smartphones or tablets that are not in de list.

      Can someone please help.

    3. I wanted to add a handy contribution for detecting tablet or mobile device
      orientation in order to serve formatted content based on the orientation of the
      device in either Portrait or Landscape mode:


      /* Specific CSS can be applied to page elements if the user is holding their device in a given orientation. This is the CSS script. */
      @media (orientation:portrait)
      {
      #isPortrait { display: block; }
      #isLandscape { display: none; }
      }
      @media (orientation:landscape)
      {
      #isPortrait { display: none; }
      #isLandscape { display: block; }
      }

      And these are the blocks you would place in your HTML page:
      <div id=”isPortrait”>
      //Show the content formatted for Portrait orientation between these Div tags.
      </div>

      <div id=”isLandscape”>
      //Show the content formatted for Landscape orientation between these Div tags.
      </div>

    4. Hi Chris,

      I’ve stripped the details out of this script and left the focus on BlackBerry devices
      only. I will be providing useful examples to other developers at the upcoming BlackBerry
      Jam conference on August 23rd and this is a fine example of Agent detection for net
      based content developers.

      Thank you in advance for the useful code snippet :)


      $BlackBerry = stripos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
      $RimTablet= stripos($_SERVER['HTTP_USER_AGENT'],"RIM Tablet");

      if($BlackBerry)
      {
      //we're a BlackBerry phone -- do something here
      }
      if($RimTablet)
      {
      //we're a RIM/BlackBerry Tablet -- do something here
      }

    5. Thanks for this, it might not work in every single case (Nokia Symbian and Windows Phone are missing, off the top of my head), and I need OS version checking for blackberrys as I plan on supporting the newer ones, but a very lightweight, usable script that gets the job done.

      • While it may not include support for all user agents out of the box it’s quite easily expanded. I’ll try and add in a few more user agents in the near future. Glad you found it useful.

      • This code simply detects the most common mobile os’s. How you use it is up to you but it does not modify how a page displays. The only OS excluded is Windows Mobile — which I will add shortly.

    6. How can the code be modified to give the user the option to view the desktop version of the site if required from their mobile device?

      Also on the conditional code section i.e.

      }else if($iPad){
      //were an iPad — do something here

      How would add a redirect here to a m.mydomain.com address ?

    7. Hello, to detect BBerries and playbook table we’d be talking about something like that:

      $blackberry = stripos($_SERVER['HTTP_USER_AGENT'],”blackberry”);

      or is there any other thing required?

      Many thanks for your solution!

    8. I think if i leave it empty the on de index.php, the computer part, then it works.

      Is there also a separate php part for only opera mini. It works different.