{"id":935,"date":"2009-06-29T21:37:17","date_gmt":"2009-06-30T02:37:17","guid":{"rendered":"http:\/\/unitstep.net\/?p=935"},"modified":"2009-06-29T22:20:59","modified_gmt":"2009-06-30T03:20:59","slug":"determine-your-visitors-location-based-on-ip-address","status":"publish","type":"post","link":"https:\/\/unitstep.net\/blog\/2009\/06\/29\/determine-your-visitors-location-based-on-ip-address\/","title":{"rendered":"Determine your visitor’s location based on IP address"},"content":{"rendered":"
If you’re running a website that provides a service, it’s likely that it would be beneficial to know a user’s location (or have a rough idea) so that the content could be tailored to their specific geographic area. But how do you get their location, without having to ask them? By using their IP address, it’s possibly to determine their general area with fairly good accuracy. In this tutorial, I’ll explain how to do that using the free IP address geolocation database<\/a> from IPInfoDB<\/a>.<\/p>\n <\/p>\n There are numerous reasons for using the user’s location to improve the quality of a service. For example, the restaurant guide and review site, Restaurantica<\/a>, automatically makes a guess as to your location and populates the “Search” field so that you are one click away from finding information tailored to your geographic area. <\/p>\n This determination is based on the user’s IP address. Usually, the lookup is done based on a static set of mappings from IP addresses to geographical locations, like cities. In fact, this is exactly what the IP address geolocation database<\/a> (that we’ll use) does. It has the advantage of being easy to understand but unfortunately the accuracy can degrade over time, as IP addresses are usually not hardwired to specific locations. For example, most DSL users are subject to DHCP, so the same IP address may be re-used across different cities. (The IP address database from IPInfoDB is updated periodically to help reduce this inaccuracy)<\/p>\n Thus, you should not rely on the information provided to be 100% accurate; it should only be used as a suggestion. This is why Restaurantica<\/a> only populates<\/em> the search field with your estimated location, rather than giving you the results for that location right away; if it was wrong, the results for a different location would certainly be confusing to a user. This gives the user a chance to correct any inaccurate lookups.<\/p>\n The first step is to download the database<\/a> and import it into your local database; for this example I’ll use MySQL. Note that the site also offers an IP location Lookup API<\/a>, which has the advantage of not requiring a local database but may be subject to usage limits; if you’ll be getting a lot of traffic you’ll be better off using the local database.<\/p>\n I downloaded the Complete (City) database as one table in SQL format<\/a>. Extracting the file from the archive, you’ll see that it’s well over 300 MB in size. If you’re fond of using phpMyAdmin<\/a>, you unfortunately won’t be able to import such a large file using that tool, so we’ll have to rely on the command line to import the contents. Go to your Running this command will take some time, as it won’t be fast having to import such a huge table<\/strong>. Take a break and come back in a few minutes. \ud83d\ude42 (You may want to create a new database to hold just this table, do that before running this command) After that’s done, you should be able to see the contents in phpMyAdmin, like below:<\/p>\n \n<\/a>\n<\/p>\n Querying the database is straightforward, and the IP Info DB website has plenty of examples at the bottom of the download page<\/a>. Basically, once you have the user’s IP, you can use a query like this to find the most likely city it originated from:<\/p>\n The INET_ATON function converts a string that is the dotted-quad octet form of an IP address (the form you’re most used to seeing) into a 32-bit unsigned integer. This integer is then used to locate the record that most likely corresponds to the geographical location. The database is structured such that row with the highest An example:<\/p>\n (This returns the city of Toronto, Ontario, Canada) If you want to get a list of likely locations, just increase the LIMIT argument to something like 10, and this will return 10 locations that are likely close to where the IP address is located. Note that there may be duplicates in this list<\/strong>.<\/p>\nMotivation and Usage<\/h2>\n
Using the IP Address geolocation database<\/h2>\n
Import the contents into MySQL<\/h3>\n
mysql\/bin<\/code> folder and enter the following command, assuming you extracted the SQL file to the same location:<\/p>\n
mysql -u root -p [name of database] < ipinfodb_one_table_full.sql<\/code><\/pre>\n
Using the database<\/h3>\n
SELECT * \r\nFROM `ip_group_city` \r\nWHERE `ip_start` <= INET_ATON([ip address as a string])\r\nORDER BY ip_start DESC\r\nLIMIT 1;<\/code><\/pre>\n
ip_start<\/code> value not greater than the given IP address is deemed to be the most likely location.<\/p>\n
SELECT * FROM `ip_group_city` WHERE `ip_start` <= INET_ATON('69.156.150.155') ORDER BY ip_start DESC limit 1;<\/code><\/pre>\n
A simple example<\/h2>\n