Parsing mobile phone user agent string , good solution or library?

DhruvPathak picture DhruvPathak · Mar 1, 2012 · Viewed 8.5k times · Source

Requirement : To find out which phone the user is using by parsing the user agent string from his browser.( in PHP ) eg. Nokia 1100 , Samsung D900 .

Is there a good library available with sufficient database of phone vendors and models ?

Some solutions I found, need your views on the same :

i) handsetdetection : https://www.handsetdetection.com - paid subscription

ii) WURFL - http://wurfl.sourceforge.net/

iii) Own solution - I have a small database of phone makers, and models , but will have to add tailormade checks for user agent strings in my code to match /fuzzy match against the database as user agent string format is not consistent across makers.

UPDATE:

We created a tailormade solution which was a mix of regular expressions to parise standard user agents like iOS,Android,Blackberry,and WURFL as a fallback mechanism for other phones like symbian,j2me,bada etc.

WURFL works great after adding database/cache (MySql,memcached,mongodb etc) which already exists as a setting in the core codebase.Though you have to update/sync wurfl information with newest version of WURFL handset xml database every few weeks to stay updated with specs of new released mobile phones.

Answer

Bjørne Malmanger picture Bjørne Malmanger · Mar 11, 2012

First, I would say KISS (Keep It Simple, Stupid) which is a widely used expression for a reason. I would start off by double checking my business needs and see how much device information I really need and what I am going to use it for. Maybe you only need to see which rendering engine the client is using?

Second, you should consider the parsing time. If you end up using i.e. WURFL and parsing that XML on your site, you would need to parse 400 000+ lines of XML when checking for device information. Of course, you could put the XML into a local indexed database, but that would also require some maintenance scripts to synchronize the data in the updated XML with the database.

Third (but maybe it should be first?) is considering the cost/benefit of the solution. If you make money on the site, it might be smart to leave some responsibility on a partner. A hosting service like handsetdetection.com seems capable of delivering high traffic at a not-so-terrifying cost. Another benefit is that they are responsible for maintaining their own repository and could lose customers if their service isn't good enough. The OpenSource community could theoretically go on a 4 month vacation and the soulution wouldn't be maintained in that period (I really wouldn't think that should be anything to worry about ;-)

Not knowing your exact needs, I would prioritize it like this:

  1. Use as simple a solution as possible, i.e. the solution from Detect Mobile Browsers
  2. Go OpenSource, like WURFL. I just love OpenSource solutions :-)
  3. If your business needs guaranteed stability and data quality, you should let the professionals handle it ;-)