Mobile Device Detection Framework (PHP)

Current Version: N/A
Price: FREE

Responsive design is the future, but it’s still necessary to perform server-side device detection in some instances. This lightweight server-side solution utilizes the php $_SERVER array (HTTP_USER_AGENT) and the php function stripos providing a framework to detect a list of known devices. Reference the code below to use as a stating point for basic php device detection and modify as required for your project. Example supported devices: iOS (iPod Touch, iPhone, iPad) Android, Windows Phone, Windows Tablet, BlackBerry <= 9, Blackberry 10, Blackberry PlayBook, WebOS, Nokia Symbian. It also provides a framework to target a form factor for known devices (phone, tablet, or other) along with specific operating systems (iOS, webOS, Blackberry, Symbian, Android, and Windows).

The code below compares the client's user agent against a known list of user agents for popular devices. It then flags the detected device(s) by setting a corresponding variable to true. The variable can then be checked when targeting specific devices.

//initialize all known devices as false
$iPod = false;
$iPhone = false;
$iPad = false;
$iOS = false;
$webOSPhone = false;
$webOSTablet = false;
$webOS = false;
$BlackBerry9down = false;
$BlackBerry10 = false;
$RimTablet = false;
$BlackBerry = false;
$NokiaSymbian = false;
$Symbian = false;
$AndroidTablet = false;
$AndroidPhone = false;
$Android = false;
$WindowsPhone = false;
$WindowsTablet = false;
$Windows = false;
$Tablet = false;
$Phone = false;

//Detect special conditions devices & types (tablet/phone form factor)
if(stripos($_SERVER['HTTP_USER_AGENT'],"iPod")){
    $iPod = true;
    $Phone = true;
    $iOS = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"iPhone")){
    $iPhone = true;
    $Phone = true;
    $iOS = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"iPad")){
    $iPad = true;
    $Tablet = true;
    $iOS = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"webOS")){

    $webOS = true;

    if(stripos($_SERVER['HTTP_USER_AGENT'],"Pre") || stripos($_SERVER['HTTP_USER_AGENT'],"Pixi")){
        $webOSPhone = true;
        $Phone = true;
    }
    if(stripos($_SERVER['HTTP_USER_AGENT'],"TouchPad")){
        $webOSTablet = true;
        $Tablet = true;
    }
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"BlackBerry")){
    $BlackBerry = true;
    $BlackBerry9down = true;
    $Phone = true;
{
if(stripos($_SERVER['HTTP_USER_AGENT'],"BB10")){
    $BlackBerry = true;
    $BlackBerry10 = true;
    $Phone = true;
{
if(stripos($_SERVER['HTTP_USER_AGENT'],"RIM Tablet")){
    $BlackBerry = true;
    $RimTablet = true;
    $Tablet = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"SymbianOS")){
    $Symbian = true;
    $NokiaSymbian = true;
    $Phone = true;
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"Android")){

    $Android = true;

    if(stripos($_SERVER['HTTP_USER_AGENT'],"mobile")){
        $AndroidPhone = true;
        $Phone = true;
    }else{
        $AndroidTablet = true;
        $Tablet = true;
    }
}
if(stripos($_SERVER['HTTP_USER_AGENT'],"Windows")){

    $Windows = true;

    if(stripos($_SERVER['HTTP_USER_AGENT'],"Touch")){
        $WindowsTablet = true;
        $Tablet = true;
    }
    if(stripos($_SERVER['HTTP_USER_AGENT'],"Windows Phone")){
        $WindowsPhone = true;
        $Phone = true;
    }
}



//Target form factors
if( $Phone ){
    //we're phone form factor
}else if( $Tablet ){
    //we're a tablet form factor
}else{
    //we're neither a phone or tablet
}




//Target operating systems
if( $iOS ){
    //we're an iOS device
}else if( $Android ){
    //we're an Android device
}else if( $Windows ){
    //we're a Windows device
}else if( $BlackBerry ){
    //we're a BlackBerry device
}else if( $webOS ){
    //we're a webOS device
}else if( $Symbian ){
    //we're a Symbian device
}else{
    //we're neither a phone or tablet
}




//Target individual devices
if( $iPod || $iPhone ){
    //we're an iPhone/iPod touch -- do something here
}else if($iPad){
    //we're an iPad -- do something here
}else if($AndroidPhone){
    //we're an Android Phone -- do something here
}else if($AndroidTablet){
    //we're an Android Tablet -- do something here
}else if($WindowsPhone){
    //we're an Windows Phone -- do something here
}else if($WindowsTablet){
    //we're an Windows Tablet -- do something here
}else if($webOSPhone){
    //we're a webOS phone -- do something here
}else if($webOSTablet){
    //we're a webOS tablet -- do something here
}else if($BlackBerry9down){
    //we're an outdated BlackBerry phone -- do something here
}else if($BB10){
    //we're an new BlackBerry phone -- do something here
}else if($RimTablet){
    //we're a RIM/BlackBerry Tablet -- do something here
}else if($NokiaSymbian){
    //we're a Nokia Symbian device -- do something here
}else{
    //we're not a known device.
}

This code is meant to be a starting point. It can be easily implemented as a class for more modular/efficient use or it can be used in it’s current form. I hope it helps you with your project!


      

Leave a Reply

22 Responses to Mobile Device Detection Framework (PHP)

  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.