November 29th, 2006

Determine a visitor's location by their IP address!

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.

5 Responses to ' Determine a visitor's location by their IP address! '

Subscribe to comments with RSS or TrackBack to ' Determine a visitor's location by their IP address! '.

  1. Tony said,

    on September 18th, 2007 at 7:28 pm

    But is there a way to determine what city (if any)?

    thanks!

    -Tony

  2. Mark said,

    on April 3rd, 2008 at 11:55 am

    You can simply use an online ip lookup like http://www.ipgp.net

  3. Jason said,

    on April 25th, 2008 at 8:07 am

    I am (http://www.Prop2Go.com) is looking for a free webservice that execpts an IP adress and returns a city, state, country. Do you know if a free webservice like this exists?

  4. Nick Owens said,

    on July 12th, 2008 at 2:07 pm

    Quick question: I am implementing the ranges into my SQL Server. Which data type do you feel is appropriate? It appears the largest number (after a quick Z -> A sort in Excel) is 4294967295 (approx. 4 billion). The data type "int" only holds numbers as large as 2 billion so it has to be "bigint". Unfortunate since that's twice the required disk space for such a large number (8 bytes instead of 4).

  5. Nick Owens said,

    on July 12th, 2008 at 2:39 pm

    Just for my edification, since I haven't checked, are there any overlapping IP number ranges to your knowledge? In other words, might the above query ever return two records and there be a need for ordering the resulting records by prefered country?

    For example, if there is an overlap, I could add the following ORDER BY statement to enforce the result for US first and CA second:

    ORDER BY CASE WHEN (COUNTRY = 'US') THEN 1 WHEN (COUNTRY = 'CA') THEN 2 ELSE 3 END ASC

Leave a reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image