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.

    6 Comments on “Mobile Device Detection Function”

    1. 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!

      [Reply]

      Chris Schiffner Reply:

      I’ll look up the BlackBerry and Playbook user agents tomorrow and update the code to add support.

      [Reply]

      Chris Schiffner Reply:

      I’ve updated the example above to detect BlackBerry’s and Playbooks. Hope it helps!

      [Reply]

      Douglas Reply:

      Thanks Chris for your contribution, great work!

      [Reply]

    2. 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 ?

      [Reply]

      Chris Schiffner Reply:

      You could simply add }else{ //do something } for a desktop version.

      As for a redirect — php’s header function is your friend: header(‘Location: http://www.example.com/‘) — http://php.net/manual/en/function.header.php

      [Reply]

    Leave a Comment

    You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>