Geo IP Introduction
Maxmind GeoIP addresses database is an excellent reference for learning where your website visitors are located. Once installed simply pass it an IP address or number query and it can return information about Country, Region, City, Longitude and Latitude co-ordinate locations. You can then manipulate this data to your advantage for statistical analysis, targeted regional advertisements, default language, currency and delivery selections the possibilities are really endless.
About Installing GeoIP
Maxmind are kind enough company to offer two solutions that query IP to country all for free, providing you quote ( "This product includes GeoLite data created by MaxMind, available from http://www.maxmind.com/." ) wherever you use their data. This article is focused on installing the CSV format for use with PHP + MySQL.
Personally I struggling to install the Geo IP database using phpMyAdmin on my remote production server and I couldn't find a tutorial for my circumstance, eventually I managed to do it but it was a very time consuming process which I wish never to go through again, you can read the article I wrote on this method here to judge for yourself Maxmind geoip setup tutorial using phpMyAdmin. It was clear that an easier solution was needed especially when Maxmind release new CSV updates at the start of every month, I didn't want to be going through that process everytime. For my benefit and others I took it upon myself to develop a re-usable script that would make maintaining a GeoIP database a simple task. This article explains how to install the re-usable PHP and MySQL script created by Bartomedia to manage your own Maxmind Geo IP database on your web server quickly and easily.
GeoIP Installation Requirements
Before you proceed you should know that this article assumes you have PHP and MySQL already installed on your web server, you should also have permission to create, edit and delete tables in MySQL. FTP access is also required so you can upload a copy of the Maxmind GeoLite Country CSV file and the PHP script to manage the GeoIP database.
PHP + MySQL GeoIP Manager
Create a new file and name it something simple like "GeoIPManager.php" then copy the following code from the grey box below and paste it into the page.
When you have created the file adjust the MySQL database connection variables to your own username, password and the name of your own database. Upload the file to the root of your web server by FTP together with the latest copy of the Maxmind Geolite countries CSV file, if you decide to rename the CSV file or upload the two files to a different location you will have to adjust the filename or filepath variables in the script.
Open your web browser and access the script you just created and uploaded to your webserver. Follow the simple onscreen instructions to install the database. Within a few minutes you should have your very own GeoIP database up and running. The script also features its own IP querying tool.
If you are having any troubles with this script please leave comments below I will reply as quickly and as best i can.
Querying GeoIP from your script
Create a new php include file, I named mine 'geofunctions.inc.php' appropriately for its purpose, copy and paste into the file the code from the grey box below.
Upload the 'geofunctions.inc.php' file to the root of your server or to any location you like. You will include this file in all the pages that you require GeoIP querying functions.
From within the page that you require to query GeoIP data use the code in the grey box below as an example.
Faster GeoIP MySQL Retrieval
Much of my geoip querying is taken from examples at http://vincent.delau.net/php/geoip.html however I have made modifications in my 'geolocations.inc.php' that search and retrieve data faster and more efficiently from comments made at J.Coles weblog following the excellent advice of Andy Skelton, Nikolay Bachiyski and also that of Mark Robson. Nikolay mentions that the GeoLite Country database actually contains lots of gaps so the script returns the country in the nearest range but it may actually be an unassigned gap, his solution was to create a script to fill the gaps with dummy rows and return "-" however this is not essential. Simply query the IP number against the returned start value if you are searching by the end value if it appears outside the range you can simply return "-" or false rather than filling in gaps.
Feel free to use the script wherever you like, if you do use it all I ask for in return is a link back to my Blog