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 &amp;amp; types (tablet/phone form factor)
    $iPod = true;
    $Phone = true;
    $iOS = true;
    $iPhone = true;
    $Phone = true;
    $iOS = true;
    $iPad = true;
    $Tablet = true;
    $iOS = true;

    $webOS = true;

    if(stripos($_SERVER['HTTP_USER_AGENT'],&quot;Pre&quot;) || stripos($_SERVER['HTTP_USER_AGENT'],&quot;Pixi&quot;)){
        $webOSPhone = true;
        $Phone = true;
        $webOSTablet = true;
        $Tablet = true;
    $BlackBerry = true;
    $BlackBerry9down = true;
    $Phone = true;
    $BlackBerry = true;
    $BlackBerry10 = true;
    $Phone = true;
if(stripos($_SERVER['HTTP_USER_AGENT'],&quot;RIM Tablet&quot;)){
    $BlackBerry = true;
    $RimTablet = true;
    $Tablet = true;
    $Symbian = true;
    $NokiaSymbian = true;
    $Phone = true;

    $Android = true;

        $AndroidPhone = true;
        $Phone = true;
        $AndroidTablet = true;
        $Tablet = true;

    $Windows = true;

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

//Target form factors
if( $Phone ){
    //we're phone form factor
}else if( $Tablet ){
    //we're a tablet form factor
    //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
    //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
    //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

Your email address will not be published. Required fields are marked *

26 Responses to Mobile Device Detection Framework (PHP)

  1. Hello,

    There is a bug on line 56 and on line 62 where the closing bracket is reversed, and so if you include this code through using require() it gives a fatal error. I just wanted to let you know!

    Thank you for the useful script! <3

  2. 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
    //we’re not a mobile device.

  3. 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.

  4. 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 id=”isLandscape”>
    //Show the content formatted for Landscape orientation between these Div tags.

  5. 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");

    //we're a BlackBerry phone -- do something here
    //we're a RIM/BlackBerry Tablet -- do something here

  6. 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.

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

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