Skip to content

Instantly share code, notes, and snippets.

@olavmrk
Created March 24, 2015 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olavmrk/1cc21f890ffc97998b84 to your computer and use it in GitHub Desktop.
Save olavmrk/1cc21f890ffc97998b84 to your computer and use it in GitHub Desktop.
Test of IPv6 GeoIP lookups.
#!/usr/bin/env php
<?php
/* Test script for GeoIP IPv6 lookups.
* Databases can be downloaded from:
* http://dev.maxmind.com/geoip/legacy/geolite/
*/
/* Path to the GeoIP library in Piwik. */
$PIWIK_GEOIP_DIR = '/source/piwik/libs/MaxMindGeoIP';
set_include_path($PIWIK_GEOIP_DIR . ':' . get_include_path());
require_once('geoip.inc');
require_once('geoipcity.inc');
$dbs = array(
'GeoLiteCity' => geoip_open('GeoLiteCity.dat', GEOIP_STANDARD),
'GeoLiteCityv6' => geoip_open('GeoLiteCityv6.dat', GEOIP_STANDARD),
);
$addrs = array(
'158.38.62.0',
'0:0:0:0:0:ffff:9e26:3e00',
'2001:700:1::',
);
function lookup_location($db, $addr) {
$isIPv6 = filter_var($addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
if ($isIPv6) {
$location = geoip_record_by_addr_v6($db, $addr);
} else {
$location = geoip_record_by_addr($db, $addr);
}
if ($location) {
return $location->country_name;
} else {
return NULL;
}
}
foreach ($dbs as $db_name => $db_handle) {
foreach ($addrs as $addr) {
$country = lookup_location($db_handle, $addr);
echo "$db_name($addr): " . var_export($country, TRUE) . "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment