Determine a visitor’s location by their IP address!

by admin on July 24, 2009 · 0 comments

Tracking a visitor's geographical location (ie. country, region, city, latitude, longitude, ZIP code, ISP and domain name) used to be a cool trick. Now it is an integral part of many websites, enabling them to identify the locations from where they're getting the maximum traffic; and tune your webpages accordingly. It also enables you to modify the site according to location, or send users to specific sections (ie. show prices in CAD for Canadian visitors).

It is relatively easy to use this technology on your website with IP2Location's proprietary IP address lookup database. This database is free for the IP-Country version. Of you need more details like region, city, latitude, longitude, ZIP code, ISP and domain name for the IP address, you need to purchase the full database.

To determine the user's location by their IP address, you need to perform these three steps:

1. Retrieve the visitors' IP address
Here are the functions to get the IP address using ASP, PHP, an .Net (C# & VB.Net).

2. Convert the visitor's IP address to an IP Number
Here are the functions to convert an IP address to an IP number using ASP, PHP, an .Net (C# & VB.Net).

3. Locate the IP Number in the IP-Country database
Example: the IP Address 202.186.13.4 converts to IP Number 3401190660. It is between the beginning and the ending of the following IP numbers:
"3401056256","3401400319","MY","MALAYSIA"

From the IP-Country recordset, the Country_Name with this IP number range is Malaysia, the Country_Code is MY.

So once you have the IP Number, here's the SQL Query to locate the matching recordset:

SELECT country_name FROM ip_to_country WHERE
[your IP number] BETWEEN ip_start AND ip_end

Of course, you need to download the IP2Country database.

Previous post:

Next post: