dotMobimobiThinkingmobiForgemobiReadyDeviceAtlasfind.mobiInstant Mobilizer

Lightweight Device-Detection in ASP

Section Feature image
Posted by ruadhan 2 years 4 days ago
Bookmark and Share

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.

 

 

 

<%
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.


Posted by ruadhan 2 years 4 days ago

ruadhan's picture

Mobile enthusiast and Editor of mobiForge

@rodono

Posted by xyz 1 year ago

ASP.NET determines the capabilities of every browser which sends requests to the server independant of if the request originates from a mobile device or a desktop computer. If browsers are detected correctly and mobile devices are identified depends on the configuration of the server.

Posted by admin 1 year ago

Quote:
ASP were already replaced by ASP.NET in 2001

Thanks for the news ;-) There are of course still many sites stilling using classic asp - it is these we are catering for here rather than ASP.NET developers.

Posted by Anonymous (not verified) 1 year ago

i'm using below function
----------------------------------
Function isMobile (ua,accept)

If(_

instr(accept,"text/vnd.wap.wml")>0 _
or instr(Lcase(ua),"symbian")>0 _
or instr(Lcase(ua),"series")>0 _
or instr(Lcase(ua),"nokia")>0 _
or instr(Lcase(ua),"mot-")>0 _
or instr(Lcase(ua),"motorola")>0 _
or instr(Lcase(ua),"lg-")>0 _
or instr(Lcase(ua),"lge")>0 _
or instr(Lcase(ua),"nec-")>0 _
or instr(Lcase(ua),"lg/")>0 _
or instr(Lcase(ua),"samsung")>0 _
or instr(Lcase(ua),"sie-")>0 _
or instr(Lcase(ua),"sec-")>0 _
or instr(Lcase(ua),"sgh-")>0 _
or instr(Lcase(ua),"sonyericsson")>0 _
or instr(Lcase(ua),"sharp")>0 _
or instr(Lcase(ua),"windows ce")>0 _
or instr(Lcase(ua),"portalmmm")>0 _
or instr(Lcase(ua),"o2-")>0 _
or instr(Lcase(ua),"docomo")>0 _
or instr(Lcase(ua),"philips")>0 _
or instr(Lcase(ua),"panasonic")>0 _
or instr(Lcase(ua),"sagem")>0 _
or instr(Lcase(ua),"smartphone")>0 _
or instr(Lcase(ua),"up.browser")>0 _
or instr(Lcase(ua),"up.link")>0 _
or instr(Lcase(ua),"googlebot-mobile")>0 _
or instr(Lcase(ua),"googlebot")>0 _
or instr(Lcase(ua),"slurp")>0 _
or instr(Lcase(ua),"spring")>0 _
or instr(Lcase(ua),"alcatel")>0 _
or instr(Lcase(ua),"sendo")>0 _
or instr(Lcase(ua),"blackberry")>0 _
or instr(Lcase(ua),"opera mini")>0 _
or instr(Lcase(ua),"opera 2")>0 _
or instr(Lcase(ua),"netfront")>0 _
or instr(Lcase(ua),"mobilephone mm")>0 _
or instr(Lcase(ua),"vodafone")>0 _
or instr(Lcase(ua),"avantgo")>0 _
or instr(Lcase(ua),"palmsource")>0 _
or instr(Lcase(ua),"siemens")>0 _
or instr(Lcase(ua),"toshiba")>0 _
or instr(Lcase(ua),"i-mobile")>0 _
or instr(Lcase(ua),"asus")>0 _
or instr(Lcase(ua),"ice")>0 _
or instr(Lcase(ua),"kwc")>0 _
or instr(Lcase(ua),"htc")>0 _
or instr(Lcase(ua),"softbank")>0 _
or instr(Lcase(ua),"mozilla/5.0 (x11; u; linux i686; en-us; rv:1.8.0.7) gecko/20060909 firefox/1.5.0.7")>0 _
or instr(Lcase(ua),"playstation")>0 _
or instr(Lcase(ua),"nitro")>0 _
or instr(Lcase(ua),"iphone")>0 _
or instr(Lcase(ua),"ipod")>0 _
or instr(Lcase(ua),"google wireless transcoder")>0 _
or instr(Lcase(ua),"t-mobile")>0 _
or instr(Lcase(ua),"obigo")>0 _
or instr(Lcase(ua),"brew")>0 _
or instr(Lcase(ua),"yahooseeker")>0 _
or instr(Lcase(ua),"msmobot")>0 _
or instr(Lcase(ua),"novarra")>0 _
or instr(Lcase(ua),"skp")>0 _
or instr(Lcase(ua),"openweb")>0 _

) _
Then

isMobile=True
else
isMobile=False
end if

End function

Posted by Kevin K (not verified) 1 year ago

You might want to optimize that a bit. ASP does not short-circuit any logical tests in an IF statement. All the tests will be done even if a match is found on the first. You should do the lcase() before the statement and maybe have a separate IF statement for the most common browsers first.

Posted by sixseraphim (not verified) 1 year ago

Can I use this code so that when a mobile user is detected that it redirects them to my more mobile friendly website?

Posted by admin 1 year ago

You sure can!

Posted by William Ramirez (not verified) 1 year ago

Doesn't work with opera, detects it as mobile even when it is a desktop.

Posted by WilliamStam (not verified) 1 year ago

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-")

look for the one "oper" i changed mine to "operamini"

Posted by shaunjbutler 1 year ago

How could you allow mobile users to visit the full site if they wanted without being automatically redirected back to the mobile site?

Posted by wulser 1 year ago

Nice article!

As admin stated earlier, many sites are still using ASP. Mine is one of them.

In reply to shaunjbutler's question, even if it was posted long ago, I can tell you that I simply changed the very last part of the code:

If mobile_browser>0 Then
   Response.Write("Mobile!")
Else
   Response.Write("Not mobile!")
End If

To

If mobile_browser>0 Then
   Response.Redirect("*/looksmobile.asp")
Else
   Response.Redirect("www.ourstandardwebsite.com")
End If

The looksmobile.asp site presents the user with a choice whether or not they want to view the mobile version of the site. Then saves their choice in a cookie, and sends them to their preferred site in the future.

Posted by stockstorm 1 year ago

Can I use this with an existing desktop browser website? The problem is, the website is in ASP.Net.

Posted by taomark 51 weeks ago

The script also doesn't work with my phone. It's a Sony Ericsson C902 with NetFront browser (a lot of new phones have similar technology). Problem is, this mobile browser returns a value for mobile_ua as "mozi" and therefore thinks it's a desktop Mozilla variant.

I know that mobile redirects work on my phone since pages like Google and bbc.co.uk implement it successfully. Any ideas? I only have the one phone on which to test my proposed mobile site so it's annoying that I can't get it to work!!

Posted by taomark 51 weeks ago

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)

Posted by ruadhan 51 weeks ago

Hmmm, it looks like there is a transcoder messing up your UA string:

The (Novarra-Vision/7.3) bit gives it away...

The usual UA strings for that device can be seen at the bottom of this page .

Ruadhan O'Donoghue
mobiForge

Posted by taomark 51 weeks ago

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?

Posted by taomark 51 weeks ago

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....

Posted by ruadhan 51 weeks ago

It's likely that you are all connecting to the Internet via the same transcoding proxy. What operator are you with?

Ruadhan O'Donoghue
mobiForge

Posted by ruadhan 51 weeks ago

taomark wrote:
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?

In this case, I would edit the line

.Pattern = "(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm)"

to include the string Netfront:

.Pattern = "(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|windows ce|pda|mobile|mini|palm|netfront)"

Although it won't work where your UA has been mangled by a transcoder.

Ruadhan O'Donoghue
mobiForge

Posted by taomark 51 weeks ago

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!

Posted by ruadhan 51 weeks ago

Quote:
We're all with Vodafone.

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!

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: live.vodafone.ie goes via the transcoder, whereas isp.vodafone.ie 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.

Ruadhan O'Donoghue
mobiForge

Posted by cw622 31 weeks ago

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

Posted by felipetv 28 weeks ago

Agent of my Blackberry 8310 Curve:
HTTP_USER_AGENT - Mozilla/4.0 (compatible; MSIE6.0; Windows NT 5.1; SV1)
Help...

Posted by mwitcomb 25 weeks ago

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 !!

Posted by ruadhan 25 weeks ago

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

Ruadhan O'Donoghue
mobiForge

Posted by Thorandor 16 weeks ago

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