Detecting mobile devices

In today’s world, serving different content to desktop and mobile clients became de facto standard, at least for newly developed web sites. It presents many new tasks before developer and one of them is how to successfully detect if visitor uses desktop of mobile client. We will set aside the discussion about what is mobile and what is not and focus only on detecting if a client is a phone/tablet or not. For that we use regular expressions with pattern that enables us search environment variable HTTP_USER_AGENT for multiple strings at once. We also check if client accepts WAP and if environment variables HTTP_PROFILE and HTTP_X_PROFILE are empty. Here’s the code:

$wbcnl[
$wbsetv[rxstr|$wbe[http_user_agent]]
$wbsetv[rxp|up.browser$wbfn[chr(124)]android$wbfn[chr(124)]up.link$wbfn[chr(124)]mmp$wbfn[chr(124)]symbian$wbfn[chr(124)]smartphone$wbfn[chr(124)]midp$wbfn[chr(124)]wap$wbfn[chr(124)]phone$wbfn[chr(124)]windows ce$wbfn[chr(124)]pda$wbfn[chr(124)]mobile$wbfn[chr(124)]mini$wbfn[chr(124)]palm$wbfn[chr(124)]avantgo$wbfn[chr(124)]bada\/$wbfn[chr(124)]blackberry$wbfn[chr(124)]blazer$wbfn[chr(124)]compal$wbfn[chr(124)]elaine]
$wbsetv[rxp|$wbgetv[rxp]$wbfn[chr(124)]fennec$wbfn[chr(124)]hiptop$wbfn[chr(124)]iemobile$wbfn[chr(124)]ip(hone$wbfn[chr(124)]od)$wbfn[chr(124)]iris$wbfn[chr(124)]kindle$wbfn[chr(124)]lge $wbfn[chr(124)]maemo$wbfn[chr(124)]netfront$wbfn[chr(124)]opera m(ob$wbfn[chr(124)]in)i$wbfn[chr(124)]phone$wbfn[chr(124)]p(ixi$wbfn[chr(124)]re)\/$wbfn[chr(124)]plucker$wbfn[chr(124)]pocket$wbfn[chr(124)]psp$wbfn[chr(124)]treo]
$wbsetv[MCount|$WBRXE[$wbgetv[rxstr]|$wbgetv[rxp]|match|t|t|t]]
$wbsetv[wap|$wbindof[$wbe[HTTP_ACCEPT]|application/vnd.wap.xhtml+xml]]
$wbsetv[lprof|$wblen[$wbe[HTTP_X_PROFILE]$wbe[HTTP_PROFILE]]]
$wbcalc[$wbgetv[MCount]+$wbgetv[wap]+$wbgetv[lprof]]
]

Now let’s analyze it:
First line contains $wbcnl function that clears line break and carriage return characters (chr 10 and chr 13) which we do not need in returned code.

In lines 2, 3 and 4 we assign values to variables rxstr (the string to be searched to which we assign the value of environment variable HTTP_USER_AGENT) and rxp (regular expression pattern that contains all string patterns that will be searched for). We split assigning value to rxp in two lines for better readability. To add new devices you should only modify line 4 by adding new device name preceded with function | (e.g. |YourNewDevicePattern) between word "treo" and square bracket.

In line 5 we execute regular expression search and store the total number of occurrences of ANY pattern string in variable "MCount" (so, if none of patterns was found MCount will be zero)

Line 6 checks if client accepts â€oapplication/vnd.wap.xhtml+xmlâ€? and assigns the result in variable â€owapâ€? (zero if it does not or positive number if it does).

Line 7 checks the length of combined environment variables HTTP_PROFILE and HTTP_X_PROFILE and assigns result to variable â€olprofâ€? (again zero if both are empty).

In line 8 we sum all three results so if any of the tests was successful the final result will be greater than zero and that would mean that client is a mobile device.

Line 9 simple closes function $wbcnl opened in first line.

Now let’s use this code for actual testing. Save the code in file named checkmoble.ic in your web’s document root and place the following line in FormFields section of page that will be used for detecting device type:

WB_Redirect=$wbif{$wbrinc{/checkmobile.ic}>0|/mobile|/desktop}

Upon processing file checkmobile.ic, if any of the tests were successful and result is not zero, WhizBase will redirect client to directory â€omobileâ€?, else it will redirect client to directory â€odesktopâ€?.

Thank you for reading; we hope you’ll find good use for this piece of code.


Whizbase lite