Detect Mobile Vistors to Your Site With This Free Script

Over at OSNews, Eugenia Loli-Queru has just updated the OSNews Mobile Detection Kit, a simple PHP script that analyzes visiting browser’s User-Agent headers and redirects them to the mobile or full version of your site. Scripts like this are a light weight/high performance alternative to more elaborate device detection packages like WURFL or Device Atlas. For blogs and news sites that have a mostly-text, “one size fits all” mobile site this script is all you need to send mobile visitors to it.

There are quite a few similar mobile device detection scripts floating around the net. Eugenia’s is by far the most complete in terms of the number of different mobile user agents it tests for. The script even recognizes text-only, legacy desktop, PDA, game console and embedded systems browsers that would choke on a typical desktop site and sends them to your mobile version. There’s a hook to optionally redirect wml-only devices to a wml version of your site and logic to make sure that search engine crawlers see your full site. It also detects those pesky transcoders and redirects them to your mobile site where you should be sending a Cache-Control No-Transform header to tell them to leave your content alone.

The script will require a little tweaking to get working on your site as the targets of the redirection are hard-coded. The README.txt file included in the download tells you everything you need to know in order to use it and has suggestions for advanced use. It’s open source so you are free to modify it to suit your needs.

Download the Mobile Detection Kit here. One word of warning. It’s fine to automatically deliver mobile formatted content to mobile browsers, but always provide users with a way to reach your full site if they want to. It’s unlikely that you mobile edition contains all the content of your desktop version. Users hate being blocked from content. Just add a “Full Version” link on the mobile site that bypasses the detection script or sets a cookie that the script can be modified to check.

6 thoughts on “Detect Mobile Vistors to Your Site With This Free Script

  1. James Pearce, thank you for the comment. The purpose of the script is not to offer attributes about phones, but to have a single bit value: “is this a non-desktop browser, or not?” That’s the question it tries to answer.

    For my site,, which gets about 150,000 pageviews daily, this script has been working admirably well. It almost never missed a browser, and our mobile page just accommodates well most of them in terms of HTML support. On the other hand, I had over 25 phones or emulators to test with. ;)

  2. For an implementation of your final paragraph (which is not quite as easy as it sounds, IMHO ;-) ), the following should work with this detection function too:

    As for this library itself, I would say it misses a few obvious tricks. The presence of a UA-Profile header is a great hint, as is the Accept header.

    The WordPress Mobile Pack contains a ‘light’ mobile switcher, which may be another alternative:

    (It benchmarks very well against isMobile() calls with a device database approach. That I even know such things suggests I should get a life.)

Comments are closed.