Mobile device detection with PHP: 2 methods compared

I’ve been working on a mobile version of the (dutch) translation website I founded. Apart from releasing the mobile site itself, it’s of course also important to make people aware of the existence of it. That’s exactly the reason why I started looking for a script that performs mobile device detection. The aim of such a script is to detect who visits the www-version of my website with a mobile device and automatically redirect those visitors to the mobile site.

Within a couple of minutes I already found a couple of php-scripts that perform the trick. After reading through the code and checking some comments on blogs I ended up with two candidates:

1) A script by Andy Moore on mobiForge
2) A script by Russell Beattie

That left me with the next dillema, which one to pick?

In order to answer this question, I defined the 2 main criteria that are important for the implementation on my website:

1) Code execution time: on a high traffic website, you want the code to be as efficient as possible
2) Mobile device coverage: detect as many mobile devices as possible (coverage)

This is probably a good time to mention the great WURFL project (open-source). WURFL is absolutely a great resource when you’re developing for mobile devices. Essentially, it’s a XML configuration file which contains information about capabilities and features of many mobile devices. I used this resource for testing the scripts above.

Testing and results

For both candidates, I looped through all user-agents in the WURFL file (9457) and tested:
– the average time required to check if this user-agent belongs to  mobile device or not
– if this user-agent is detected as a mobile device or not

And the results are…

Average execution time
– mobiForge script: 0,0078 seconds
– Russell’s script: 0,0021 seconds

Device coverage
– mobiForge scripts: 82,8%
– Russell’s scripts: 81,9%

In other words, Russell’s script runs faster but the script from mobiForge has a slightly better coverage. However, I think the small difference in coverage can almost be neglected since the biggest part of the mobile web users are all using one of the popular mobile devices (N95, HTCs etc). Both scripts have absolutely no problem detecting these. Furthermore, you can easily extend the list of mobile user-agents in both scripts (I already added the iPhone). Based on these findings, I decided to implement Russell’s script and it’s working just fine.

Oh… and in case you’re wondering, the mobile version of vertalen.nu will be released within the coming week.

Tagged , , ,

Leave a Reply

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