Your server's PHP is compiled without IPv6 support (using the --disable-ipv6
) so your code fails, however it should work fine on a server on which PHP supports IPv6.
Here's how to check if IPv6 is supported on your PHP installation, taken from here :
if (defined('AF_INET6')) {
echo "PHP was compiled without --disable-ipv6 option";
} else {
echo "PHP was compiled with --disable-ipv6 option";
}
By the way, here's how I would rewrite your code, it looks better to me but I'm far from being a PHP expert so use it at your own risk.
function getISOcode($visitorIP) {
if(filter_var($visitorIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$geoIPdb = "GeoIP.dat";
include_once($this->maxmindDirectory."geoip.inc");
$gi = geoip_open($this->maxmindDirectory.$geoIPdb, GEOIP_STANDARD);
return geoip_country_code_by_addr($gi, $visitorIP);
} elseif (filter_var($visitorIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$geoIPdb = "GeoIPv6.dat";
include_once($this->maxmindDirectory."geoip.inc");
$gi = geoip_open($this->maxmindDirectory.$geoIPdb, GEOIP_STANDARD);
return geoip_country_code_by_addr($gi, $visitorIP);
} else {
return false; // or throw an exception about the address being invalid
}
}