|
Page 1 of 2
Knowing where a website visitor is located is vital if you hope to have a chance to provide information that is geographically relevant. In the case of I Programmer, the problem was to pinpoint a user's location so that best Amazon locale could be used to retrieve book pricing details in the correct currency and shipping rates. The obvious solution is to ask the user but, guess what, users really don't like to be asked things. Indeed if confronted by a pop-up window demanding that they divulge where they are they might just go away without ever finding out if the site offers anything useful.
For some reason most users are paranoid about revealing any information, no matter how innocent and harmless even if it could be beneficial. So, we need an automatic way to determine with reasonable accuracy where the user is. We also need a facility to override the choice - users for some reason are always pleased to correct personal data that you have wrong even if they won't supply it in the first place!
Google Gears Gelolocation API
The most obvious thing to do is to use some sort of IP address to location mapping, but this isn't the only option. You can make use of GPS, WiFi location and, if the client is connected via mobile phone, cell location. In fact it makes good sense to put all of these methods together to provide a more accurate location.
This is exactly what the Google Gears Gelolocation API does and in the future or for applications that need very accurate location information - as good as 200m -this might be the way to go. But for many applications having to include yet another JavaScript library and work with another API and online provider is just too much. I opted for a simpler solution that was quicker to implement and didn't add any load to the clients network connection as the whole job is don't on the server via a PHP script. In the future I might have to re-think.
Using MaxMind's GeoLite
In principle all we need to do the job is a big table mapping IP address to location. Sadly there isn't a standard 100% certified IP to location table. As a result a number of companies have put in a lot of effort and created exactly such tables and of course sell them and or their services. One such company is MaxMind which offers a range of location services including the free GeoLite Country. This is a downloadable database complete with access software. There is an upgrade to a paid service which increases the accuracy from 99.5% to 99.8% and throws in additional features. The free version is certainly enough to prove the worth of the idea. If you need to locate your users to an individual city then there is also GeoLite City and its corresponding paid-for full version.
The first thing to do is to download the GeoLite Country Binary Format from the MaxMind website
http://www.maxmind.com/app/geolitecountry
This takes the form of a .gz compressed file and you need to decompress this, use 7-Zip if you don't already have a decompression utility. The resulting file, GeoIP.dat, is currently around 1Mbyte and this has to be stored somewhere accessible on your web server. As I'm working with a Joomla PHP based website I stored it in:
/includes/GeoLite
To work with it from PHP you also need to download geoip.inc which contains the code needed to access the database. This also needs to be stored on the server and the most sensible locate is in the same directory as the database, i.e. in my case, /includes/GeoLite.
<ASIN:0596007035>
<ASIN:0321518667>
<ASIN:0470097779>
<ASIN:0596006810>
<ASIN:0782143334>
|