Detecting Mobile Devices Using PHP

Don't you wish there were an easy way to detect whether your web site visitor is connecting with a desktop PC or a mobile device of some sort?

The good news is that using PHP (or another server-based scripting language), it's sort of easy to detect some devices by parsing the UserAgent string. Because the code is run on the server rather than in the browser itself (such as JavaScript), these PHP-based techniques yield better reliability in detecting mobile devices. Just remember that there are lots of important caveats, including:

  • These techniques depend on the contents of the UserAgent string. As a result, these techniques may not work if the browser is emulating a different one (e.g., some mobile browsers can be set to emulate Internet Explorer).
  • Mobile operators can also change the UserAgent contents when the device is customized for their networks and added to their stock device portfolio. Operators also sometimes change the UserAgent value as the page request passes through their networks.
  • UserAgent strings are moving targets. Once implemented, you'll need to monitor the effectiveness of your code against the list of your high priority devices or platforms.


Introducing the "uagent_info" Class

I created a PHP class called "uagent_info" to encapsulate the logic for detecting mobile devices. This class is easy to use and its API is highly modularized so that you can detect broad classes of devices (such as smartphones or WAP/WMP-capable devices), specific platforms (such as the iPhone/iPod Touch, Symbian S60 or BlackBerry).

Here is a sample block of the code so you can see how it is organized:

// The uagent_info class encapsulates information about
//   a browser's connection to your web site. 
//   The object's methods return 1 for true, or 0 for false.
class uagent_info
   //Stores some info about the browser and device.
   var $useragent = "";

   //Stores info about what content formats the browser can display.
   var $httpaccept = ""; 

   // Standardized (and configurable) values for true and false.
   var $true = 1;
   var $false = 0;

   // A long list of strings which provide clues 
   //   about devices and capabilities.
   var $deviceIphone = 'iphone';
   var $deviceIpod = 'ipod';

   // [ SNIP! Other variables snipped out ] 

   //The constructor. Initializes several default variables.
   function uagent_info()
       $this->useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
       $this->httpaccept = strtolower($_SERVER['HTTP_ACCEPT']);

   //Returns the contents of the User Agent value, in lower case.
   function Get_Uagent()
       return $this->useragent;

   // Detects if the current device is an iPhone.
   function DetectIphone()
      if (stripos($this->useragent, $this->deviceIphone) > -1)
         //The iPod touch says it's an iPhone! So let's disambiguate.
         if ($this->DetectIpod() == $this->true)
            return $this->false;
            return $this->true; 
         return $this->false; 

   // [ SNIP! Other functions snipped out ] 



Using the "uagent_info" Class

First, instantiate the uagent_info object, then call one of its functions. The device detection functions return 1 for true or 0 for false. It's as simple as that! Here's an example:

//Instantiate the object to do our testing with.
$uagent_obj = new uagent_info();

//Let's detect an iPhone.
//This will return a 1 for true or 0 for false.
$iPhone = $uagent_obj->DetectIphone();

//Do some logic with it now, such as print it.
print("<p>You're using an iPhone: ".$iPhone."</p>");

//You might also want to print out the user agent string.
$agent = $uagent_obj->Get_Uagent();
print("<p>Your user agent string: <br /> ".$agent."</p>");


Introducing MobileESP!

Since the original publication of this article in 2008, we've seen interest grow dramatically for this easy-to-use mobile detection library for web site publishers. As a result, we have spun this code library off into a separate free open source (Apace 2 license) code library called MobileESP. We've continuously expanded the capabilities of the PHP code library as new devices came out. So, for the latest information, including updates, bug fixes, new features, and more, please see the project web site: Here are a few quick links:

  • Get MobileESP: Download the latest version of the MobileESP code libraries! Learn more.
  • The MobileESP API: See the complete list of mobile detection functions which are largely consistent across supported web technologies (PHP, Java, APS.NET, Ruby, and JavaScript).
  • Live MobileESP API Demos: Want to see how the MobileESP APIs work with your device? Check out the PHP & JavaScript live demos!
  • Download Samples: There are several sets of sample web pages you can download to see how easy it is to integrate MobileESP into your web site. Learn more.
  • License Information: MobileESP is free to use, though donations are greatly appreciated and encouraged. For more information, please visit the Licensing page on the MobileESP web site.



MobileESP is easy to implement and generally great if you're primarily concerned about the class of device (e.g., smartphone or not, phone vs. tablet), or the mobile platform (e.g., iPhone or Symbian S60). If you need detailed device information or usage metrics, you may wish to check out WURFL (free!) or


Support MobileESP!

If you find the MobileESP code library useful, please consider donating so we can purchase additional devices on our testing wishlist! Donations of $25-50 are greatly appreciated!


MobileESP Project

Learn more about the new MobileESP project!

We've spun off our mobile device detection code into a FREE open source project called MobileESP! Learn more about it on the new project web site.

Please Donate

If you like and use this code, please consider donating. Any amount is appreciated!

(Please donate -- help us purchase more devices on our testing wishlist!)

About Us

Hand Interactive offers user experience, usability, and business strategy expertise. We're passionate about crafting engaging experiences for mobile, desktop, and web users.

Contact us for more information.