1

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.

  1. Is this an error in my code, or down to my server/PHP configuration? (When I checked AF_INET6 was undefined.)

  2. 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.

4

1 回答 1

4

您的服务器的 PHP 是在不支持 IPv6 的情况下编译的(使用--disable-ipv6),因此您的代码会失败,但是它应该可以在 PHP 支持 IPv6 的服务器上正常工作。

以下是检查您的 PHP 安装是否支持 IPv6 的方法,取自此处

if (defined('AF_INET6')) {
  echo "PHP was compiled without --disable-ipv6 option";
} else {
  echo "PHP was compiled with --disable-ipv6 option";
}

顺便说一句,这就是我将如何重写您的代码,它看起来更好,但我远不是 PHP 专家,所以使用它需要您自担风险。

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
    }
}
于 2014-03-28T15:51:40.583 回答