Created
March 24, 2015 11:53
-
-
Save olavmrk/1cc21f890ffc97998b84 to your computer and use it in GitHub Desktop.
Test of IPv6 GeoIP lookups.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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