ASP Code, ASP.Net Code, PHP Code, SQL Code, Web

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 converts to IP Number 3401190660. It is between the beginning and the ending of the following IP numbers:

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.



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



  2. You can simply use an online ip lookup like

  3. I am ( 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. 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. 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:


  6. smithveg-ergonomic

    Is there any method to determine the STATE in a country?

  7. Really very helpful information till find the country name.
    i appriciate you admin.

  8. looking for a good web service for this purpose rather than using the database on my own hosting…

  9. Thanks for the information, this is really what I need. In order to improve my site security, I am developing on a log application that will determine any visitor action while they are at my site. So, their geographical location will be a very important thing for the app. Do you have any information on the place that sells the complete database? Thanks.

  10. Hi!
    Thanks very much.
    But I don't know how to import this .csv file to mysql database.
    Can you upload .sql file ^^!

  11. how can i find my friend ip address via chai

  12. Can this service be used to display relevant banner ads to a site visitor?

  13. Why re-invent the wheel?

    gives everything you need. Without an ? it gives the info for the current browser.

    Being a newbie to all this, I wish someone would give me the code to parse the three-line output, or extract the data from the xml.


  14. How to know location of ip address
  15. Cool! Just going to drop that into the code for my website (, it'll save at least some people from having to go through the dreaded flags page!

  16. I was wondering how to get the results of this ip2location to be used to complete an address form for users who want to register in a website. In this case they dont need to select their country and state/province and it is being selected automatically.

  17. Hello, I have applied and downloaded the IP2Country Database but I noticed it isn't complete. I checked my IP address, printed it and looked for it in the database. After 2113929216 2130706431 JP JPN JAPAN the thing that followed is already
    2147549184 2147942399 US USA UNITED STATES.. it skipped a lot of numbers and the one I was looking for happened to be in the missing numbers. Is there a way I can get a full copy. Actually all I need to save right now is the IP of middle east countries.

  18. Justin Cook – you are an angel. Thanks for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image