I'm developing an open source plugin for use 3rd party sites. It includes this snippet to obtain a visitors country code:
$visitorIP = $_SERVER['REMOTE_ADDR'];
if( filter_var($visitorIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ) {
$geoIPdb = 'GeoIP.dat';
} elseif ( filter_var($visitorIP, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ) {
$geoIPdb = 'GeoIPv6.dat';
} else return 'something';
include_once($this->maxmindDirectory . 'geoip.inc');
$gi = geoip_open($this->maxmindDirectory . $geoIPdb, GEOIP_STANDARD);
if($geoIPdb == 'GeoIP.dat') {
$ISOcode = geoip_country_code_by_addr($gi, $visitorIP); }
else {
$ISOcode = geoip_country_code_by_addr_v6($gi, $visitorIP);
}
My server and ISP both use IPv4 and the code works for IPv4 addresses; but I can't test real visitor IPv6 addresses.
However, if I test by hard coding $visitorIP
as an IPv6 address I get an "inet_pton() [function.inet-pton]: Unrecognized address" warning.
Is this an error in my code, or down to my server/PHP configuration? (When I checked AF_INET6 was undefined.)
If its not my error, will this code work correctly on servers where $_SERVER['REMOTE_ADDR'] contains an IPv6 address, or do I still need to add additional checks?
Thanks for any advice.
return
ing something. – user2629998