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.
Great post, Jochem. Really interesting speed comparison. Can you let us in on the code/method you used to loop through the WURFL file?
Hi Jochem,
both of the scripts you mention are a great start…but there’s also a strategy we’re calling “Not-Device” Detection that turns these models upside down.
We’ve found that it’s faster, gives greater coverage and is easier to maintain. In fact the only real maintenance we do is for specific bots. And provided you present a link back to the PC site then most bots will find their way there on their own.
I’ve just added a blog post that discusses it in more detail:
http://MobileOnlineBusiness.com.au/blog?pb
roBman
Can you either explain how you added the iPhone or just post the code? I have been using Andy Moore’s code which already has a link for the iPhone but it doesn’t seem to recognize the iPhone 3Gs.
@shibu just search the User-Agent header for “iPhone” or “iPod” that should get all iPhone like devices.
I’d second Rob’s approach – generally it seems faster – combined with a few tricks like checking for the existence of a HTTP X-WAP-PROFILE header and the contents of the HTTP ACCEPT header you won’t get many misses. Check out the algorithm here:
http://notnotmobile.appspot.com/#what
Cheers,
John
I have published the new version of Apache Mobile Filter, now the filter is give to you the information of capabilities as apache environment.
Now you can develope in any language (php,jsp, ruby etc.) and have the information of mobile capability.
Read more info here: http://www.idelfuschini.it/it/apache-mobile-filter-v2x.html