Home | Services | Portfolio | Resources | About

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? (This question may sound familiar...)

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 technqniques 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. They may also change the UserAgent value when the device connects to the network.
  • 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.

Alternatives

The PHP code in this article is great if you're only concerned about the class of device (e.g., smartphone or WAP/WML), 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 HandsetDetection.com or WURFL (see below for more info).

 

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

 

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.
//Returns 1 for true or 0 for false.
$iPhone = $uagent_obj->
DetectIphone();

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

//You can also get the user agent
$agent = $uagent_obj->
Get_Uagent();
print('<p>Your user agent string: '.$agent.'</p>');

 

Let's put the PHP code through its paces below!

 

Detect iPhone & iPod Touch

Use the following code to detect whether the device viewing the page is an iPhone and/or iPod Touch. And don't forget: iPod Touches are devices, too!

  • You're using an iPhone, DetectIphone(): false
  • You're using an iPad, DetectIpad(): false
  • You're using an iPod Touch, DetectIpod(): false
  • You're using an iPhone or iPod Touch, DetectIphoneOrIpod(): false

 

Detect Symbian S60 Smartphones

The most popular smartphone platform in the world is Symbian S60. Used primarily by Nokia and a few other manufacturers, S60 features an extremely capable browser.

  • You're using the WebKit browser on an S60 device, DetectS60OssBrowser(): false
  • You're using any Symbian OS-based device, including older S60, UIQ devices (loaded with Opera by default), or the S60 WAP browser, DetectSymbianOS(): false

 

Detect Android Devices

Besides the iPhone, the only other mobile device in recent times to receive as much attention, both from geeky fanboys and the general public, has been the T-Mobile G1, often called the "Google Phone." The G1 is powered by a new mobile phone operating system called Android, which Google has sponsored. Android is a smartphone-class OS with an advanced browser that is similar to Mobile Safari on the iPhone in capabilities.

Why detect this browser? The native Android browser is extremely capable and also based on WebKit, which underlies Mobile Safari & the S60 browser.

Test: You're using an Android device: false

 

Detect Windows Mobile Devices

Devices running Windows Mobile are fairly popular in the U.S., especially among corporate IT departments. This code detects both the non-touch screen (Standard/Smartphone) and touch screen (Professional/PocketPC) types of devices.

Test: You're using a Windows Mobile device, DetectWindowsMobile(): false

 

Detect BlackBerry Devices

The most popular smartphone platform in the United States. (Still beats the iPhone in sales!) Unfortunately, the BlackBerry browser isn't great.

Test: You're using a BlackBerry, DetectBlackBerry(): false

Test: You're using a BlackBerry Touch (Storm 1/2), DetectBlackBerryTouch(): false

 

Detect PalmOS Devices

Last, but not least, in the smartphone category are PalmOS devices. These devices are still fairly popular in the U.S., if less so abroad, and there is a large installed base of PalmOS devices.

Test: You're using a WebOS device, DetectPalmWebOS(): false

Test: You're using a PalmOS device, DetectPalmOS(): false

 

Additional API Testing

Now that we've seen the general pattern for how to use our PHP uagent_info object, let's see the full list of what it can detect. In the interest of space, we won't show the code for each. (But you can download it above!)

  • This is any smartphone-class device, DetectSmartphone(): false
  • This device's browser is based on WebKit, DetectWebkit(): false
  • Quick check to see if this is a mobile device. Ought to detect most modern mass market phones and smartphones, DetectMobileQuick(): false
  • Longer, more thorough check to see if this is a mobile device. Ought to include the Nokia Internet Tablet and game consoles, DetectMobileLong(): false
  • This device's browser supports WAP/WML content, DetectWapWml(): false
  • This device supports Java MIDP, DetectMidpCapable(): false
  • This device is BREW-powered, DetectBrewDevice(): false
  • This is a Danger Hiptop, DetectDangerHiptop(): false
  • This is a Nokia Internet Tablet (770, N800, or N810), DetectMaemoTablet(): false
  • This is a Sony Mylo, DetectSonyMylo(): false
  • This is an Archos media player/tablet, DetectArchos(): false
  • This is a game console (e.g., Xbox, Wii, Nintendo DS, PSP), DetectGameConsole(): false

 

Your UserAgent String

It would probably help to know what your browser is reporting in it's UserAgent string...

"ccbot/1.0 (+http://www.commoncrawl.org/bot.html)"

 

HandsetDetection.com

If you have the technical chops, you might consider registering with a very good (and free!) service called HandsetDetection.com and integrating their service into your site. The service can tell you not only what class of device is visiting your site, but also what the manufacturer and model number are, tons of additional details (e.g., screen size), and even the geolocation of the device. Plus, the site offers lots of reporting metrics about visitor devices. Hand Interactive doesn't currently have any affiliation with them, but we thought you should know about this cool service anyway!

 

WURFL

The most comprehensive source for mobile device (browser and otherwise) is probably WURFL. It's an excellent free & open source project with an active user community that is constantly updating its device database. WURFL is also a more heavy weight implementation than our PHP code, but can provide detailed handset statistics. (HandsetDetection.com also uses WURFL!)

 

UserAgent String Resource

Zytrax has a fairly comprehensive list of UserAgent values for mobile devices: www.zytrax.com/tech/web/mobile_ids.html

 

Enjoy the fun at home!

Download the PHP code from our new Google Code page!

Note: We've casually tested this code on all of the major smartphone platforms and a handful of mass market phones, plus the Nokia Internet Tablets. So far so good! That being said, the most important caveat is that you should thoroughly test the code for yourself based on your own needs and expectations -- and always using your high priority target devices!

License Info & Costs

We have moved this code library to a Google Code hosting project and changed the license to an Apache License 2.0, which better fits a free, open source project like this. We've also rechristened this project "MobileESP." For more info, please visit the new MobileESP site: www.mobileESP.org


<- Back to Resources Overview