Device detection is the first step in performing content adaptation. Here we cover lightweight device detection using classic ASP (VBScript). This is based on the PHP version found here. It is a simple script which will detect most mobile browsers. However, if you need more information about the properties of the device such as screen width and height, image format support etc. so that you can tailor your content to specific devices, then you need something more than this. In this case you should consider using a full device properties database such as DeviceAtlas.
This script attempts to match the requesting UA string against about 90 well-known mobile browser UA string snippets, and a couple of special cases. It also checks some of the other HTTP headers for hints that the client is mobile.
If you simply need to decide if the client is a mobile or desktop browser, then this script is for you! If you need to know more about a requesting device, try DeviceAtlas. Improvements and suggestions are very welcome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<% Option Explicit Dim user_agent, mobile_browser, Regex, match, mobile_agents, mobile_ua, i, size user_agent = Request.ServerVariables("HTTP_USER_AGENT") mobile_browser = 0 Set Regex = New RegExp With Regex .Pattern = "(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm)" .IgnoreCase = True .Global = True End With match = Regex.Test(user_agent) If match Then mobile_browser = mobile_browser+1 If InStr(Request.ServerVariables("HTTP_ACCEPT"), "application/vnd.wap.xhtml+xml") Or Not IsEmpty(Request.ServerVariables("HTTP_X_PROFILE")) Or Not IsEmpty(Request.ServerVariables("HTTP_PROFILE")) Then mobile_browser = mobile_browser+1 end If mobile_agents = Array("w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs", "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-", "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port", "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tosh", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw", "xda", "xda-") size = Ubound(mobile_agents) mobile_ua = LCase(Left(user_agent, 4)) For i=0 To size If mobile_agents(i) = mobile_ua Then mobile_browser = mobile_browser+1 Exit For End If Next If mobile_browser>0 Then Response.Write("Mobile!") Else Response.Write("Not mobile!") End If %> |
To use this script, simply include it in any page for which you wish to determine whether the client is mobile or desktop.
Note that this script uses Option Explicit. If you want to use this script with code that does not use Option Explicit, you should comment out the first two lines to avoid compilation errors.
31 Comments
If it helps, the HTTP_USER_AGENT for my phone is:
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7 MG(Novarra-Vision/7.3)
Hmmm, it looks like there is a transcoder messing up your UA string:
The [code](Novarra-Vision/7.3)[/code] bit gives it away…
The usual UA strings for that device can be seen at the bottom of this page .
Fair point, but the first two strings (at the bottom of that reference page) would surely also return a value for mobile_ua of “mozi” and therefore if my UA string was one of them, I’d have the same problem. Or am I reading that wrong?
BTW, I have checked with 3 of my friends who all have the same model of phone and all 3 display the same UA string as mine! Which, as you pointed out, isn’t listed on the page you referenced. So either the UA strings on that page are out of date, or C902 phones are now displaying something different! Very annoying….
It’s likely that you are all connecting to the Internet via the same transcoding proxy. What operator are you with?
[quote=taomark]Fair point, but the first two strings (at the bottom of that reference page) would surely also return a value for mobile_ua of “mozi” and therefore if my UA string was one of them, I’d have the same problem. Or am I reading that wrong?[/quote]
In this case, I would edit the line
[code language=”php”]
.Pattern = “(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm)”[/code]
to include the string Netfront:
[code language=”php”]
.Pattern = “(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm|netfront)”
[/code]
Although it won’t work where your UA has been mangled by a transcoder.
We’re all with Vodafone. At the moment I’m using a workaround by inserting the line:
If InStr(Request.ServerVariables(“HTTP_USER_AGENT”), “Novarra”) > 0 Then mobile_browser = mobile_browser+1
after the for…next loop. This will detect all Novarra devices (which are mobile or handheld anyway). Not the best solution but it works for now. Just wondered if there was a neater way of doing this, especially since there might be other mangled UA strings out there!
[quote]We’re all with Vodafone. [/quote]
Vodafone uses the novarra transcoder…
[quote]This will detect all Novarra devices (which are mobile or handheld anyway). Not the best solution but it works for now. Just wondered if there was a neater way of doing this, especially since there might be other mangled UA strings out there![/quote]
Welcome to the world of transcoders – unsolicited transcoding is the bane of mobile web developers, and the mere mention of them is enough to cause blood vessels to burst in some developer circles.
Usually you can change the APN setting of your phone so that you don’t go via the Vodafone transcoder. However, the average user won’t know how to do this, or even what an APN is, or even what a transcoder is, or that something unintended by the content provider is happening… In Ireland, the APN: [code]live.vodafone.ie[/code] goes via the transcoder, whereas [code]isp.vodafone.ie[/code] does not transcode.
Same applies in the UK as far as I know, but I’m not sure of the details of the Vodafone UK APNs.
If you felt compelled, you might visit http://ta-da.mobi to test your phone after you update your APN setting.
in the above code
> mobile_agents = Array
I don’t see the iphone listed as an agent?? however it detects the iphone as a mobile device.
What agent defines an Iphone?
I don’t want to treat the Iphone as a mobile device
Agent of my Blackberry 8310 Curve:
HTTP_USER_AGENT – Mozilla/4.0 (compatible; MSIE6.0; Windows NT 5.1; SV1)
Help…
How would I amend the original script so that if the source was an iPHONE it would not include it as a mobile device, as all this shows is XML !!
You could try adding the string “ipho” to the list of strings above to check for – I’ve no idea if this will conflict with any desktop browser UAs though
Hiya,
Just a quick note, if you are using the script posted in the 3rd comment you need to make sure that you do not use this line:
or instr(Lcase(ua),"ice")>0
Reason is that this may cause issues with IE:
Request.UserAgent dump for ie7:
+++++Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; OfficeLiveConnector.1.3; OfficeLivePatch.0.0)
Alex
i am having problems with this script when it tries to load the site in the IE7 browser. it seems like it takes forever to load. i thought maybe it was having a conflict with some other scripts on the page, so i commented out the first two lines as suggested. however, i get the same result. is there a hack to make IE ignore the script?
Hello Guys
For above sample code
– It may not catch all mobile browsers as there are a lot of them.
– You need to keep updating user agents when new devices are introduced.
– Not easy to parse user agents to get detailed information about the mobile device such as the manufacturer, model, screen height & width, and image formats supported. This type of information is necessary to customize page layout for specific mobile device.
Please take a look at [url=http://51degrees.codeplex.com/]http://51degrees.codeplex.com/[/url]
it provides auto redirection to mobile optimized pages when request is coming from mobile device. It makes use of WURFL mobile device database. For redirection there is no need to modify existing ASP/ASP.NET web application pages as above.
Apart from this it also gives upto date mobile capability information like manufacturer, model, screen height & width, image formats supported etc., which helps you to customize your pages for best mobile output.
I have found this very useful for my mobile web development.
Please give a try it will help you a lot.
Thanks
Amit Patel
The latest release of http://51degrees.codeplex.com/ supports Multiple Mobile Redirect Locations.
Below is sample web.config settings to show how easy it is to implement mobile detection and rediection without modifying any of your existing asp.net web pages.
In this example MobileDeviceManufacturer is used as the property. This properties are exposed through the HttpRequest classes Browser property. Both WURFL capabilities and ASP.NET Browser properties can be used with the property attribute. If none of the match and the requesting device is a mobile device then the mobileHomePageUrl will be used.
[code]
[/code]
Great script. I’ve been using it for a few years.
My Question: How would I exclude a device? Like the iPad.
I appreciate your efforts and time.
Hi Ive just added webos to the Regex for the latest Palm Pre and generation of webOS devices and this seems now to recognise Palm Pre with webos
added webos
ith Regex
.Pattern = “(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm|webos)”