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.

 

 

 

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

  • taomark says:

    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)

  • ruadhan says:

    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 .

  • taomark says:

    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?

  • taomark says:

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

  • ruadhan says:

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

  • ruadhan says:

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

  • taomark says:

    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!

  • ruadhan says:

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

  • cw622 says:

    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

  • felipetv says:

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

  • mwitcomb says:

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

  • ruadhan says:

    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

  • Thorandor says:

    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

  • flckrngstar says:

    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?

  • amimpat says:

    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

  • amimpat says:

    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]

  • chilliplow says:

    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.

  • dnivdniv says:

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

Exclusive tips, how-tos, news and comment

Receive monthly updates on the world of mobile dev.

Other Products

Market leading device intelligence for the web, app and MNO ecosystems
DeviceAtlas - Device Intelligence

Real-time identification of fraudulent and misrepresented traffic
DeviceAssure - Device Verification

A free tool for developers, designers and marketers to test website performance
mobiReady - Evaluate your websites’ mobile readiness

© 2024 DeviceAtlas Limited. All rights reserved.

This is a website of DeviceAtlas Limited, a private company limited by shares, incorporated and registered in the Republic of Ireland with registered number 398040 and registered office at 6th Floor, 2 Grand Canal Square, Dublin 2, Ireland