Starting > Designing > Developing > Testing > Running
Home | News | Blogs | About

Lightweight Device-Detection in ASP

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 ago

Switcher iconSwitch to our desktop site | mobiThinking