Monday, January 22, 2007

Two Scripts to Detect Mobile Browsers

When writing mLearning content, sometimes it is good to have a single URL that all your participants can access. The following are two scripts that you can add into the head content of your page that will detect a mobile browser and automatically redirect your users to a 'mobile friendly' version of your site.


Script #1

In this script, change the $location attribute to the page you want your users to see if they are using one of the mobile devices listed in the if(stristr.. string.

Script #2

This is a simpler script to detect Windows CE browsers (although you can detect any browser with this script.) Simply change the window.location URL to your mobile friendly site.


In both of these cases, I'm redirecting the site to the mobile version of this site-

I will be adding this script to in the next few days
to help all our visitors with mobile devices.


Leonard said...

Hey Matthew; good, useful post, but I'm wondering about the purpose of the second conditional in the first set of code. Couldn't you optimise it thus?

if (stristr($ua, "Windows CE") or stristr($ua, "AvantGo") or stristr($ua, "Mazingo") or stristr($ua, "Mobile") or stristr($ua, "T68") or stristr($ua, "Syncalot") or stristr($ua, "Blazer"))
{ $location='';
header('Location: '.$location);

This would remove the second conditional and make it a bit simpler.


Matthew Nehrling said...

Good catch Leonard. I will test it both ways with several devices to see if the DeviceType conditional is needed.

M. Ford said...

great script. I'm using

(script language='JavaScript' type="text/JavaScript">
var isCE = navigator.appVersion.indexOf("Windows CE")>0;
if (isCE)
w/ minor adjustments

How do you go about modifying the script so that it will detect other devices such as a black berry, iphone, etc. Thanks!

Anonymous said...

another alternative would be to use a service like

Anonymous said...

nice post

Anonymous said...

Nice posting... easy to download

satuiku said...

i like this articlee

Mike said...

I've created a similar set of scripts in an article I wrote entitled Mobile Browser Detection and Redirection with PHP... Here's a link:

There is a link for downloading the example scripts.