Hi,
I thought I would post on how to use MaxMind geo-IP with MODx in order to identify which country your users are coming from and act accordingly.
Here are the steps:
1. DL all of the MaxMind PHP library files from
http://geolite.maxmind.com/download/geoip/api/php/
2. Store them in your library path (e.g. /var/www/libraries/MaxMind)
3. DL the latest MaxMind geo-IP database from
http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
4. Extract the GeoIP.dat database file to /var/www/libraries/MaxMind/data/GeoIP.dat (or whatever your library path is)
5. Now create a new snippet called maxmind.inc.php in a folder called maxmind with the following contents:
<?php
/**
* This is a MaxMind loader for MODx Evolution
* Author: Alex Dean (@alexatkeplar http://www.keplarllp.com)
*/
define('LIBRARY_PATH', '/var/www/libraries/'); // Update as appropriate
// Include the required PHP file
require_once LIBRARY_PATH . 'MaxMind/geoip.inc';
// Convenience function to return the path to the MaxMind geo-IP data
function getMaxMindDataFile() {
return LIBRARY_PATH . "MaxMind/data/GeoIP.dat";
}
6. Now to get the two letter country code for your site visitor, use the following code in a MODx snippet:
<?php
require_once($modx->config['base_path'] . 'assets/snippets/maxmind/maxmind.inc.php');
// First get the country code using MaxMind geo-IP lookup
$mm = geoip_open(getMaxMindDataFile(), GEOIP_STANDARD);
$ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
$countryCode = geoip_country_code_by_addr($mm, $ip);
geoip_close($mm);
echo $countryCode; // 'GB', 'US' or similar
7. Final step: setup a monthly cron job to fetch the latest geo-IP database from MaxMind (as the IP->geography mapping gets regularly updated).
I think that’s it! You can use a proxy service to test if the geo-IP is working. Hope you find this useful.