IP address to country, region, city, latitude, longitude, ZIPcode, timezone, ISP, domain and netspeed in ASP

IP2Location 0 Tallied Votes 402 Views Share

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

<%
'---------------------------------------------------------------------------
' Title        : IP Address to Country, Region, City, Latitude, Longitude, ZIPCode, TimeZone, ISP, Domain & NetSpeed Lookup Service using Database
' Description  : This script lookup the country, region, city, latitude, longitude, zipcode, timezone, isp, domain & netspeed from an IP address.
'                Things that we can do with this script.
'                		1. Display native language and currency 
'                		2. Redirect based on country 
'                		3. Digital rights management 
'                		4. Prevent password sharing and abuse of service 
'                		5. Reduce credit card fraud 
'                		6. Web log stats and analysis 
'                		7. Auto-selection of country on forms 
'                		8. Filter access from countries you do not do business with 
'                		9. Geo-targeting for increased sales and click-thrus 
'                		10. And much, much more! 
' Requirements : ASP 3.0+ and MS-SQL
' Installation : a. Create a web directory in IIS.
'				 b. Copy ipcountryregioncitylatitudelongitudezipcodetimezoneispdomainnetspeed.asp into a web directory.
'				 c.	Setup SQL Database in Microsoft SQL Server. See next section.
'				 d.	Browse ipcountryregioncitylatitudelongitudezipcodetimezoneispdomainnetspeed.asp using HTTP protocol.
'					example: http://localhost/ip2location/ipcountryregioncitylatitudelongitudezipcodetimezoneispdomainnetspeed.asp
'				 e.	Enter an IP address range between 12.4.254.0 and 12.5.76.63 and click submit.
' Setup Database (Microsoft SQL Server) :
'				 a.	Run Microsoft SQL Server's Query Analyzer.
'				 b.	File -> Open -> MsSql-Script.sql.
'				 c.	Press F5 to run the script.
'				 d.	Run Microsoft SQL Server's Enterprise Manager.
'				 e.	Goto IP2Location's Database.
'				 f.	Right click -> All Tasks -> Import Data -> Click Next -> Select text file as datasource ->
'					Click browse file button -> IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED.SAMPLE.CSV -> Click Next ->
'					Select Delimited option, ANSI as File type, {CR}{LF} as Row delimited, Double Quote {"} as Text qualifier ->
'					Click Next -> Select Comma option -> Click Next -> 
'					Select Microsoft OLE DB Provider for SQL Server as Destination, Choose IP2Location as Database ->
'					Click Next -> Click Next -> Checked Run immediately -> Click Next -> Click Finish -> Click Done
'
' Author       : IP2Location.com
' URL          : http://www.ip2location.com
' Email        : sales@ip2location.com
'
' Copyright (c) 2002-2005 IP2Location.com
'---------------------------------------------------------------------------

	Response.Write	"  <form action=""" & Request("SCRIPT_NAME") & """ method=""POST"">" & vbCrLf
	Response.Write	"    <textarea name=""ipaddress"" cols=15 rows=6></textarea><br>" & vbCrLf
	Response.Write	"    <input type=""submit"" name=""submit"" value=""submit"">" & vbCrLf
	Response.Write	"  </form>" & vbCrLf
	
	If Not Request.Form("ipaddress") = "" Then
		Dim arripaddress
		Dim i
		dim intCount
		' get the IP address from the form 
		ipaddress = Request.Form("ipaddress")
		ipaddress = Replace(ipaddress," ",vbCrLf)
		arripaddress = Split(ipaddress,vbCrLf)
		intCount = Cint(Ubound(arripaddress))
		If intCount >= 0 Then
			Response.Write "<p>"
			Response.Write "<h1><u>Lookup Result</u></h1>"
			' display Header
			Response.Write "<table border = 1>"
			Response.Write "<tr>"
			Response.Write "<td align=center>IP Address</td>"
			Response.Write "<td align=center>Country Name (Short)</td>"
			Response.Write "<td align=center>Country Name (Long)</td>"
			Response.Write "<td align=center>Region Name</td>"
			Response.Write "<td align=center>City Name</td>"
			Response.Write "<td align=center>Latitude</td>"			
			Response.Write "<td align=center>Longitude</td>"
			Response.Write "<td align=center>ZIP Code</td>"
			Response.Write "<td align=center>Time Zone</td>"
			Response.Write "<td align=center>ISP Handler</td>"
			Response.Write "<td align=center>Domain Name</td>"
			Response.Write "<td align=center>Net Speed</td>"
			Response.Write "</tr>"
			For i = 0 to intCount
				If arripaddress(i) <> "" Then			
					ipno = Dot2LongIP(arripaddress(i))
					' check if the IP address is supported in demo version
					If (ipno < 201653760) or (ipno > 201673791) Then
						Response.Write	"IP address " & ipaddress & " is not supported in demo version.<br>" & vbCrLf
					Else
						Dim conn, rs, sql, strconn
						' select MS-SQL database using DSNless connection
						strconn = "Provider=SQLOLEDB;Data Source=127.0.0.1;Initial Catalog=IP2Location;User ID=sa;Password="
						Set conn = Server.CreateObject("ADODB.Connection")
						conn.open strconn
				
						' query string to lookup the country, region, city, latitude, longitude, zipcode, timezone, isp, domain & netspeed by matching the range of IP address number
						strsql = "SELECT * FROM IPCITYLATLONGZIPTIMEISPDOMAINSPEED WHERE " & ipno & " BETWEEN ipFROM AND ipTO"
					
						' execute the query
						Set rs = conn.execute(strsql)

						' display results
						If Not rs.EOF Then
							Response.Write("<tr>")
							Response.Write("<td align=center>" & arripaddress(i) & "</td>")
							Response.Write("<td align=center>" & rs("countrySHORT") & "</td>")
							Response.Write("<td align=center>" & rs("countryLONG") & "</td>")
							Response.Write("<td align=center>" & rs("ipREGION") & "</td>")
							Response.Write("<td align=center>" & rs("ipCITY") & "</td>")
							Response.Write("<td align=center>" & rs("ipLATITUDE") & "</td>")
							Response.Write("<td align=center>" & rs("ipLONGITUDE") & "</td>")
							Response.Write("<td align=center>" & rs("ipZIPCODE") & "</td>")
							Response.Write("<td align=center>" & rs("ipTIMEZONE") & "</td>")
							Response.Write("<td align=center>" & rs("ipISP") & "</td>")
							Response.Write("<td align=center>" & rs("ipDOMAIN") & "</td>")
							Response.Write("<td align=center>" & rs("ipNETSPEED") & "</td>")
							Response.Write("</tr>")
						Else
							Response.Write("<tr>")
							Response.Write("<td align=center>" & arripaddress(i) & "</td>")
							Response.Write("<td align=center>-</td>")
							Response.Write("<td align=center>-</td>")
							Response.Write("<td align=center>-</td>")
							Response.Write("<td align=center>-</td>")
							Response.Write("<td align=center>-</td>")
							Response.Write("<td align=center>-</td>")
							Response.Write("<td align=center>-</td>")
							Response.Write("<td align=center>-</td>")
							Response.Write("<td align=center>-</td>")
							Response.Write("<td align=center>-</td>")
							Response.Write("<td align=center>-</td>")
							Response.Write("</tr>")
						End If
						rs.close
						set rs = nothing
						conn.close
						Set conn = nothing
					End If
				End If
			Next
			Response.Write("		</table>" & vbCrLf)
			Response.Write("		</p>" & vbCrLf)
		Else
			Response.Write("Please enter ip address.")
		End if
	End If


	' Convert dotted IP address into IP number in long
	Function Dot2LongIP (ByVal DottedIP)
		Dim i, pos
		Dim PrevPos, num
		If DottedIP = "" Then
			Dot2LongIP = 0
		Else
			For i = 1 To 4
				pos = InStr(PrevPos + 1, DottedIP, ".", 1)
				If i = 4 Then 
					pos = Len(DottedIP) + 1
				End If
				num = Int(Mid(DottedIP, PrevPos + 1, pos - PrevPos - 1))
				PrevPos = pos
				Dot2LongIP = ((num Mod 256) * (256 ^ (4 - i))) +   Dot2LongIP
			Next
		End If
	End Function
%>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.