mobiForge
Ad

Lightweight Device-Detection in PHP

One problem that keeps cropping up when developing mobile content is how to differentiate between mobile devices and desktop browsers. We need to be able to do this before we can think about content adaptation. The "proper" way to do this is to use a full device detection database such as DeviceAtlas (see our tutorial here) but the following method may be sufficient to get you started.

Update: We have an improved version of the algorithm which includes tablet detection here.

This PHP code implements a mobile device detection algorithm that works for a good percentage of mobile browsers out there. This code is the work of Andy Moore. The algorithm used is fairly lightweight—the code is mostly based on a list of about 90 well-known mobile browser User-Agent string snippets, with a couple of special cases for Opera Mini, the W3C Default Delivery Context and some other Windows browsers. The code also looks to see if the browser advertises WAP capabilities as a hint.

In our experience, this code does a fairly good job. It could probably be improved but it's certainly not a bad start, and is lightweight enough not to cause major problems. For more accurate device detection and increased performance we recommend using a dedicated device detection such as DeviceAtlas. Doing so will result in much higher performance, reduced load on your server and much better accuracy.

The changes changes made from Andy's original version are:

  • Adding the W3C User-Agent string (Default Delivery Context)
  • Special case detection for Opera Mini
  • Catch-all exception for devices with Windows in the UA string (Opera 9 for Windows was being recognised as a mobile device)
  • Android User-Agent snippet added

If anyone has any improvements on this code, or implementations for other languages, please let us know!

See also an ASP version.

Other Products

Market leading device intelligence for the web, app and MNO ecosystems
DeviceAtlas - Device Intelligence

Real-time identification of fraudulent and misrepresented traffic
DeviceAssure - Device Verification

A free tool for developers, designers and marketers to test website performance
mobiReady - Evaluate your websites’ mobile readiness

© 2024 DeviceAtlas Limited. All rights reserved.

This is a website of DeviceAtlas Limited, a private company limited by shares, incorporated and registered in the Republic of Ireland with registered number 398040 and registered office at 6th Floor, 2 Grand Canal Square, Dublin 2, Ireland