17 April 2008

a lightweight mobile device dedection with classic asp


I'm using below function to detect mobile phones coming to my sites.




<%

Function isMobile (ua,accept,ip)



'array accept headers

'--------------------------------------------------------------------------------------------------------

uaAcceptArray=Array( _

"application/vnd.wap.xhtml+xml","text/vnd.wap.wml")





'array known user agents

'--------------------------------------------------------------------------------------------------------

uaArray= Array( _

"symbian" , "series" , "nokia" , "mot-" , "motorola" , "lg-" , "lge" , "nec-" , "lg/" , "samsung" , "sie-" , _

"sec-" , "sgh-" , "sonyericsson" , "sharp" , "windows ce" , "portalmmm" , "o2-" , "docomo" , "philips" , _

"panasonic" , "sagem" , "smartphone" , "up.browser" , "up.link" , "googlebot-mobile" , "googlebot-image" , _

"slurp" , "spring" , "alcatel" , "sendo" , "blackberry" , "opera mini" , "opera 2" , "netfront" , "mobilephone mm" , _

"vodafone" , "avantgo" , "palmsource" , "siemens" , "toshiba" , "i-mobile" , "asus" , "ice" , "kwc" , "htc" , "softbank" , _

"playstation" , "nitro" , "iphone" , "ipod" , "google wireless transcoder" , "t-mobile" , "obigo" , "brew" , "yahooseeker" , _

"msmobot" , "novarra" , "skp" , "openweb" , "zte")





'array common network ips in case we miss user agent

'--------------------------------------------------------------------------------------------------------

ipArray=Array( _

"12.168.76" , "123.49.3" , "125.18.23" , "125.214.1" , "125.5.123.99" , "138.188.101" , "139.7.29" , "148.233.151" , _

"149.254.192" , "149.254.20" , "149.254.200" , "170.51.255" , "190.13.98" , "190.80.12" , "193.108.214" , "193.109.81" , _

"193.113.200" , "193.225.154" , "193.230.161" , "193.253.141" , "193.35.12" , "193.35.13" , "194.176.32" , "194.204.2" , _

"194.48.124" , "195.115.116" , "195.189.14" , "195.222.56" , "195.229.236" , "195.55.47" , "196.11.241" , "196.192.81" , _

"196.200.88" , "196.201.208" , "196.202.236" , "196.207.3" , "196.207.4" , "196.29.161" , "196.30.118" , "196.46.120" , _

"200.179.66" , "200.35.64" , "200.36.161" , "200.40.246" , "200.43.139" , "200.48.69" , "200.49.193" , "200.49.206" , _

"200.72.211" , "200.85.31.72" , "200.85.32" , "200.91.165" , "201.195.82" , "201.24.96" , "202.1.192" , "202.138.126" , _

"202.152.240" , "202.159.21" , "202.160.42" , "202.172.247" , "202.22.194" , "202.3.213" , "202.3.217" , "202.56.4" , _

"202.56.7" , "202.56.7.133" , "202.65.247" , "202.73.198" , "202.73.206" , "202.75.133" , "202.81.69" , "202.93.36" , _

"203.127.153" , "203.135.39" , "203.145.131" , "203.145.134" , "203.162.12" , "203.171.19" , "203.177.91" , "203.189.187" , _

"203.196.249" , "203.20.33.97" , "203.20.35" , "203.78.115" , "203.82.67" , "203.88.1" , "203.88.8" , "203.88.9" , _

"203.92.128" , "203.92.154" , "203.94.229" , "206.47.78" , "208.131.186" , "209.167.5." , "209.183.32" , "209.183.5" , _

"210.212.16" , "210.212.215" , "210.212.31.53" , "212.118.14" , "212.169.66" , "212.17.14" , "212.183.13" , "212.200.246" , _

"212.200.65" , "212.23.97" , "212.252.234" , "212.51.126" , "212.65.63.1" , "212.9.19" , "212.91.99" , "213.132.255" , _

"213.143.52" , "213.149.104" , "213.150.18" , "213.151.208" , "213.161.153" , "213.161.85" , "213.226.13" , "213.229.249" , _

"213.233.128" , "213.42.21" , "213.55.130" , "216.68.79" , "216.9.250.1" , "217.113.74" , "217.116.110" , "217.168.176" , _

"217.171.129" , "217.174.67" , "217.175.164" , "217.200.200" , "217.31.234" , "217.65.192" , "217.71.32" , "217.72.231" , _

"220.231.12" , "221.132.117" , "41.208.11" , "41.208.48.1" , "41.208.50" , "41.220.70" , "41.223.57" , "58.145.190" , _

"58.163.175" , "60.10.2.77" , "62.140.140" , "62.169.66" , "62.201.129" , "62.217.247" , "62.40.38" , "62.44.158" , _

"62.87.37" , "62.87.94" , "66.102.186" , "66.150.33" , "66.54.118" , "66.94.27" , "66.94.9." , "72.46.126.117" , _

"78.93.104.132" , "79.143.100" , "80.125.176." , "80.232.117" , "80.244.97" , "80.250.148" , "80.251.192" , _

"80.251.207.10" , "80.251.207.11" , "80.87.92" , "81.169.63" , "81.18.116" , "81.226.216" , "81.23.22" , "81.237.203" , _

"81.93.86" , "82.113.12" , "82.132.13" , "83.174.40.1" , "83.224.64" , "83.229.15" , "84.241.226" , "85.90.235" , _

"87.101.240" , "87.252.158" , "88.81.159" , "91.140.199" , "91.140.212" , "91.94.217")



'set variables

'--------------------------------------------------------------------------------------------------------

isMobile = False

ua=LCase(ua)

uaArraySize = Ubound(uaArray)

uaAccept=Lcase(accept)

uaAcceptArraySize = Ubound(uaAcceptarray)

ipArraySize = Ubound(ipArray)

uaCheck = True

ipCheck = True

'httpheadersCheck is ready but i dont use it. it's not essential

'httpheadersCheck = True



'do the math

'first check accept headers to see if device is phone.

'if not set uaCheck = True so next for runs

'--------------------------------------------------------------------------------------------------------

For i=0 To uaAcceptArraySize

If instr(uaAcceptArray(i),accept)>0 Then

isMobile=True

'debug

'response.write "<b>uaAccept</b> " &uaAcceptArray(i) &" checked as = " & isMobile&"</br>"

uaCheck = False

ipCheck = False

'httpheadersCheck=False

Exit For

End If

Next






If uaCheck = True then

For x=0 To uaArraySize

If instr(ua,uaArray(x))>0 then

isMobile=True

'debug

'response.write "<b>uaCheck</b> " & uaArray(x) &" checked as = " & isMobile&"</br>"

ipCheck = False

'httpheadersCheck=False

Exit For

End If

Next

End if





If ipCheck = True then

For y=0 To ipArraySize

If instr(ip,ipArray(y))>0 then

isMobile=True

'debug

'response.write "<b>ipCheck</b> " &ipArray(y) &" checked as = " & isMobile&"</br>"

'httpheadersCheck=False

Exit For

End If

Next

End if






'If httpheadersCheck=True Then

'If isEmpty(Request.ServerVariables("HTTP_X_UP_SUBNO"))=False Then

'isMobile=True

'End If

'If isEmpty(Request.ServerVariables("HTTP_XID"))=False Then

'isMobile=True

'End If

'If isEmpty(Request.ServerVariables("HTTP_CLIENTID"))=False Then

'isMobile=True

'End If

'If isEmpty(Request.ServerVariables("HTTP_X_PROFILE"))=False Then

'isMobile=True

'End If

'End If





End Function

%>



Comments:
to save you some time, you might also want to consider one of the existing services out there such as www.handsetdetection.com These systems allow for live detection, but also lots of other information such as location and screen size - allowing a lot more to be done and optimized based on each device visiting the site.
 
web services are ok but if you got a mobile site that has 4-5 thousand hits a day that meand 4-5 thousand server side requests to remote server.
 
Post a Comment





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]