User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP section within the Web Development category of DaniWeb, a massive community of 332,773 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,823 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP advertiser: Lunarpages ASP Web Hosting
Nov 14th, 2006
Views: 5,812
This is ASP script to enable lookup country, region, city, latitude, longitude, ZIPcode, Timezone and netspeedomain and netspeed from IP address by using IP2Location database. Free sample database available at http://www.ip2location.com.
The script supports several database types such as Microsoft Access, MS SQL and mySQL. Internet geolocation has been widely used in the products or projects below.
1) Select the geographically closest mirror
2) Analyze your web server logs to determine the countries
3) Credit card fraud detection
4) Software export controls
5) Display native language and currency
6) Prevent password sharing and abuse of service
7) Geo-targeting in advertisement
asp Syntax
  1. <%
  2. '---------------------------------------------------------------------------
  3. ' Title : IP Address to Country, Region, City, Latitude, Longitude, ZIPCode, TimeZone, ISP, Domain & NetSpeed Lookup Service using Database
  4. ' Description : This script lookup the country, region, city, latitude, longitude, zipcode, timezone, isp, domain & netspeed from an IP address.
  5. ' Things that we can do with this script.
  6. ' 1. Display native language and currency
  7. ' 2. Redirect based on country
  8. ' 3. Digital rights management
  9. ' 4. Prevent password sharing and abuse of service
  10. ' 5. Reduce credit card fraud
  11. ' 6. Web log stats and analysis
  12. ' 7. Auto-selection of country on forms
  13. ' 8. Filter access from countries you do not do business with
  14. ' 9. Geo-targeting for increased sales and click-thrus
  15. ' 10. And much, much more!
  16. ' Requirements : ASP 3.0+ and MS-SQL
  17. ' Installation : a. Create a web directory in IIS.
  18. ' b. Copy ipcountryregioncitylatitudelongitudezipcodetimezoneispdomainnetspeed.asp into a web directory.
  19. ' c. Setup SQL Database in Microsoft SQL Server. See next section.
  20. ' d. Browse ipcountryregioncitylatitudelongitudezipcodetimezoneispdomainnetspeed.asp using HTTP protocol.
  21. ' example: http://localhost/ip2location/ipcountryregioncitylatitudelongitudezipcodetimezoneispdomainnetspeed.asp
  22. ' e. Enter an IP address range between 12.4.254.0 and 12.5.76.63 and click submit.
  23. ' Setup Database (Microsoft SQL Server) :
  24. ' a. Run Microsoft SQL Server's Query Analyzer.
  25. ' b. File -> Open -> MsSql-Script.sql.
  26. ' c. Press F5 to run the script.
  27. ' d. Run Microsoft SQL Server's Enterprise Manager.
  28. ' e. Goto IP2Location's Database.
  29. ' f. Right click -> All Tasks -> Import Data -> Click Next -> Select text file as datasource ->
  30. ' Click browse file button -> IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED.SAMPLE.CSV -> Click Next ->
  31. ' Select Delimited option, ANSI as File type, {CR}{LF} as Row delimited, Double Quote {"} as Text qualifier ->
  32. ' Click Next -> Select Comma option -> Click Next ->
  33. ' Select Microsoft OLE DB Provider for SQL Server as Destination, Choose IP2Location as Database ->
  34. ' Click Next -> Click Next -> Checked Run immediately -> Click Next -> Click Finish -> Click Done
  35. '
  36. ' Author : IP2Location.com
  37. ' URL : http://www.ip2location.com
  38. ' Email : sales@ip2location.com
  39. '
  40. ' Copyright (c) 2002-2005 IP2Location.com
  41. '---------------------------------------------------------------------------
  42.  
  43. Response.Write " <form action=""" & Request("SCRIPT_NAME") & """ method=""POST"">" & vbCrLf
  44. Response.Write " <textarea name=""ipaddress"" cols=15 rows=6></textarea><br>" & vbCrLf
  45. Response.Write " <input type=""submit"" name=""submit"" value=""submit"">" & vbCrLf
  46. Response.Write " </form>" & vbCrLf
  47.  
  48. If Not Request.Form("ipaddress") = "" Then
  49. Dim arripaddress
  50. Dim i
  51. dim intCount
  52. ' get the IP address from the form
  53. ipaddress = Request.Form("ipaddress")
  54. ipaddress = Replace(ipaddress," ",vbCrLf)
  55. arripaddress = Split(ipaddress,vbCrLf)
  56. intCount = Cint(Ubound(arripaddress))
  57. If intCount >= 0 Then
  58. Response.Write "<p>"
  59. Response.Write "<h1><u>Lookup Result</u></h1>"
  60. ' display Header
  61. Response.Write "<table border = 1>"
  62. Response.Write "<tr>"
  63. Response.Write "<td align=center>IP Address</td>"
  64. Response.Write "<td align=center>Country Name (Short)</td>"
  65. Response.Write "<td align=center>Country Name (Long)</td>"
  66. Response.Write "<td align=center>Region Name</td>"
  67. Response.Write "<td align=center>City Name</td>"
  68. Response.Write "<td align=center>Latitude</td>"
  69. Response.Write "<td align=center>Longitude</td>"
  70. Response.Write "<td align=center>ZIP Code</td>"
  71. Response.Write "<td align=center>Time Zone</td>"
  72. Response.Write "<td align=center>ISP Handler</td>"
  73. Response.Write "<td align=center>Domain Name</td>"
  74. Response.Write "<td align=center>Net Speed</td>"
  75. Response.Write "</tr>"
  76. For i = 0 to intCount
  77. If arripaddress(i) <> "" Then
  78. ipno = Dot2LongIP(arripaddress(i))
  79. ' check if the IP address is supported in demo version
  80. If (ipno < 201653760) or (ipno > 201673791) Then
  81. Response.Write "IP address " & ipaddress & " is not supported in demo version.<br>" & vbCrLf
  82. Else
  83. Dim conn, rs, sql, strconn
  84. ' select MS-SQL database using DSNless connection
  85. strconn = "Provider=SQLOLEDB;Data Source=127.0.0.1;Initial Catalog=IP2Location;User ID=sa;Password="
  86. Set conn = Server.CreateObject("ADODB.Connection")
  87. conn.open strconn
  88.  
  89. ' query string to lookup the country, region, city, latitude, longitude, zipcode, timezone, isp, domain & netspeed by matching the range of IP address number
  90. strsql = "SELECT * FROM IPCITYLATLONGZIPTIMEISPDOMAINSPEED WHERE " & ipno & " BETWEEN ipFROM AND ipTO"
  91.  
  92. ' execute the query
  93. Set rs = conn.execute(strsql)
  94.  
  95. ' display results
  96. If Not rs.EOF Then
  97. Response.Write("<tr>")
  98. Response.Write("<td align=center>" & arripaddress(i) & "</td>")
  99. Response.Write("<td align=center>" & rs("countrySHORT") & "</td>")
  100. Response.Write("<td align=center>" & rs("countryLONG") & "</td>")
  101. Response.Write("<td align=center>" & rs("ipREGION") & "</td>")
  102. Response.Write("<td align=center>" & rs("ipCITY") & "</td>")
  103. Response.Write("<td align=center>" & rs("ipLATITUDE") & "</td>")
  104. Response.Write("<td align=center>" & rs("ipLONGITUDE") & "</td>")
  105. Response.Write("<td align=center>" & rs("ipZIPCODE") & "</td>")
  106. Response.Write("<td align=center>" & rs("ipTIMEZONE") & "</td>")
  107. Response.Write("<td align=center>" & rs("ipISP") & "</td>")
  108. Response.Write("<td align=center>" & rs("ipDOMAIN") & "</td>")
  109. Response.Write("<td align=center>" & rs("ipNETSPEED") & "</td>")
  110. Response.Write("</tr>")
  111. Else
  112. Response.Write("<tr>")
  113. Response.Write("<td align=center>" & arripaddress(i) & "</td>")
  114. Response.Write("<td align=center>-</td>")
  115. Response.Write("<td align=center>-</td>")
  116. Response.Write("<td align=center>-</td>")
  117. Response.Write("<td align=center>-</td>")
  118. Response.Write("<td align=center>-</td>")
  119. Response.Write("<td align=center>-</td>")
  120. Response.Write("<td align=center>-</td>")
  121. Response.Write("<td align=center>-</td>")
  122. Response.Write("<td align=center>-</td>")
  123. Response.Write("<td align=center>-</td>")
  124. Response.Write("<td align=center>-</td>")
  125. Response.Write("</tr>")
  126. End If
  127. rs.close
  128. set rs = nothing
  129. conn.close
  130. Set conn = nothing
  131. End If
  132. End If
  133. Next
  134. Response.Write(" </table>" & vbCrLf)
  135. Response.Write(" </p>" & vbCrLf)
  136. Else
  137. Response.Write("Please enter ip address.")
  138. End if
  139. End If
  140.  
  141.  
  142. ' Convert dotted IP address into IP number in long
  143. Function Dot2LongIP (ByVal DottedIP)
  144. Dim i, pos
  145. Dim PrevPos, num
  146. If DottedIP = "" Then
  147. Dot2LongIP = 0
  148. Else
  149. For i = 1 To 4
  150. pos = InStr(PrevPos + 1, DottedIP, ".", 1)
  151. If i = 4 Then
  152. pos = Len(DottedIP) + 1
  153. End If
  154. num = Int(Mid(DottedIP, PrevPos + 1, pos - PrevPos - 1))
  155. PrevPos = pos
  156. Dot2LongIP = ((num Mod 256) * (256 ^ (4 - i))) + Dot2LongIP
  157. Next
  158. End If
  159. End Function
  160. %>
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 9:25 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC