Determine Web Visitors Country of Origin in the Drop Down List using ASP and MS-SQL 2008 Database

With the emergence of online technologies such as the Internet, people and businesses have increased their reliance and use of these mediums as an avenue for commerce as it can be more convenient. During the transaction online, there are times when it is important to preset the web visitor's country of origin, ZIP code, ISP and domain name at the drop down list to prevent fraud and to ease the complexity of registration task. This article shows you how by using ASP and MS-SQL Technology, it can be done.

Let us take a simple example of a user login from Canada and he needs to fill up a shopping cart. The form may be quite complex as some businesses need more information to prevent fraud. In this case, there are needs to preset certain info in the drop down list such as country of origin, ZIP code, ip and domain name of where the users login. As a result, the drop down list in this example will preset to Cananda, with the correct zip code and ip address.

In this tutorial, we use the IP2Location™ IP-Country database to lookup country of origin from the visitor's IP address.

Step 1: Start the SQL Server Management Studio. Run the following SQL commands.

CREATE DATABASE ip2location;
USE ip2location;

Step 2: Create 'ip_country' table

CREATE TABLE [ip2location].[dbo].[ip_country] (
			[ip_from] bigint NOT NULL,
			[ip_to] bigint NOT NULL,
			[country_code] nvarchar(2) NOT NULL,
			[country_name] nvarchar(64) NOT NULL
			) ON [PRIMARY]
			GO

Step 3. Import the data into the table.

			BULK INSERT [ip2location].[dbo].[ip_country]
				FROM 'C:\[path to your CSV file]\IP-COUNTRY.CSV'
				WITH
				(
					FORMATFILE = 'C:\[path to your DB1.FMT file]\DB1.FMT'
				)
			GO
			

NOTE: You will need to copy the FMT code below and save it as a file named DB1.FMT on your computer.

			10.0
			5
			1 SQLCHAR 0 1 "\"" 0 first_double_quote  Latin1_General_CI_AI
			2 SQLCHAR 0 20 "\",\"" 1 ip_from ""
			3 SQLCHAR 0 20 "\",\"" 2 ip_to ""
			4 SQLCHAR 0 2 "\",\"" 3 country_code Latin1_General_CI_AI
			5 SQLCHAR 0 64 "\"\r\n" 4 country_name Latin1_General_CI_AI
			

Run the below SQL command to create a clustered index on the ip_to field.

			CREATE UNIQUE CLUSTERED INDEX [ip_to] ON [ip2location].[dbo].[ip_country]
			(
				[ip_to] ASC
			) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
			

The full version of IP-Country database is available for subscription at $49/year at http://ip2location.com/databases/db1-ip-country.

Sample Code:

" & vbcrlf

arr1 = split(countries, "#")

for each x in arr1
	arr2 = split(x, "|")
	countrylist = countrylist & "" & vbcrlf
next

countrylist = countrylist & vbcrlf & ""

response.write countrylist

%>

Do you like this article? Share it with others by clicking the social media buttons below. We will write more articles related to this topic.