PHP中有关IPV4 和IPV6地址转换以及其它一些常见问题

这里主要介绍一下 IPV4 / IPV6 在 PHP / MySQL 中如何转换。以及中间容易碰到的一些问题。

  • 首先介绍两个函数:

ip2long:将 IPV4 的字符串互联网协议转换成长整型数字

int ip2long ( string $ip_address )

long2ip:将长整型转化为字符串形式带点的互联网标准格式地址(IPV4)

string long2ip ( int $proper_address )

问题一:MySQL 中如何存储IP地址。

  • IPV4 地址长度32位,有 2^32-1 个地址。 所以 MySQL 中如果使用 int 来存储,要加 unsigned 标识。
  • int 有符号的范围是 -2^31 (-2,147,483,648) 到 2^31 - 1 (2,147,483,647) ,无符号的范围是 0 到 2^32-1(4294967295)
  • IPV6 地址长度128位。因此不能使用 int 存储,可以使用 varchar 类型存储。

问题二:ip2long 出现负数问题。

示例:

$ip_long = ip2long('192.168.8.30');
$long_ip = long2ip($ip_long);
echo $ip_long;    // -1062729698
echo $long_ip; // 192.168.8.30

查看PHP手册后,发现手册上是这么介绍的:

Because PHP’s integer type is signed, and many IP addresses will result in negative integers on 32-bit architectures, you need to use the “%u” formatter of sprintf() or printf() to get the string representation of the unsigned IP address.
因为PHP的 integer 类型是有符号,并且有许多的IP地址将导致在32位系统的情况下为负数, 你需要使用 “%u” 进行转换通过 sprintf() 或printf() 得到的字符串来表示无符号的IP地址。

解决办法:

$ip_long = sprintf('%u', ip2long('192.168.8.30'));
echo $ip_long; // 3232237598

接着又发现一个新问题,如果是通过 “%u” 进行转换后再调用 long2ip,会提示错误:

long2ip() expects parameter 1 to be integer,string given

接着查手册,PHP手册上是这么介绍的:

On 32-bit architectures, casting integer representations of IP addresses from string to integer is not suppossed to give correct results for numbers which exceed PHP_INT_MAX.
在 32 位架构中,从 string 转换 integer 整型形式的 ip 地址将有可能导致错误的结果,因为结果数字超出了 PHP_INT_MAX 限制。

最终解决办法,封装两个方法:

/**
 * 代替 long2ip 函数
 * @param $ip
 * @return string
 */
function convertIpToString($ip)
{
    $long = 4294967295 - ($ip - 1);
    return long2ip(-$long);
}


/**
 * 代替 ip2long 函数
 * @param $ip
 * @return string
 */
function convertIpToLong($ip)
{
    return sprintf("%u", ip2long($ip));
}

测试调用:

$ip_long = $this->convertIpToLong('192.168.8.30');
$long_ip = $this->convertIpToString($ip_long);

echo $ip_long; // 3232237598
echo $long_ip; // 192.168.8.30

问题三:MySQL 中怎么转换 IP 地址。

  • MySQL 中提供了几个函数,INET_ATON 将 IPV4 地址转换为整数。 INET_NTOA 将整数转换为 IPV4 地址。
  • 如果是 IPV6 地址也有对应的方法:INET6_ATON 和 INET6_NTOA,这两个方法需要 5.6 以上版本才能使用。

调用示例:

SELECT INET_ATON('192.168.8.30');       // 3232237598
SELECT INET_NTOA('3232237598');         //  192.168.8.30

问题四:PHP 中怎么处理 IPV6 地址。

  • PHP 中没有直接提供函数实现 IPV6 地址的转换。 不过PHP手册中提供了两个方法可以实现这一需求。要运行这两个方法首先需要开启 php_gmp.dll 模块。
/**
 * IPV6 地址转换为整数
 * @param $ipv6
 * @return string
 */
function ip2long6($ipv6)
{
    $ip_n = inet_pton($ipv6);
    $bits = 15; // 16 x 8 bit = 128bit
    $ipv6long = '';
    while ($bits >= 0) {
        $bin = sprintf("%08b", (ord($ip_n[$bits])));
        $ipv6long = $bin . $ipv6long;
        $bits--;
    }
    return gmp_strval(gmp_init($ipv6long, 2), 10);
}


/**
 * 整数转换为 IPV6 地址
 * @param $ipv6long
 * @return string
 */
function long2ip6($ipv6long)
{

    $bin = gmp_strval(gmp_init($ipv6long, 10), 2);
    if (strlen($bin) < 128) {
        $pad = 128 - strlen($bin);
        for ($i = 1; $i <= $pad; $i++) {
            $bin = "0" . $bin;
        }
    }
    $bits = 0;
    $ipv6 = '';
    while ($bits <= 7) {
        $bin_part = substr($bin, ($bits * 16), 16);
        $ipv6 .= dechex(bindec($bin_part)) . ":";
        $bits++;
    }
    // compress
    return inet_ntop(inet_pton(substr($ipv6, 0, -1)));
}

测试调用:

$ip6_long = $this->ip2long6('2001:4860:a005::68');
$long_ip6 = $this->long2ip6($ip6_long);
echo $ip6_long; // 42541956150894553250710573749450571880
echo $long_ip6; // 2001:4860:a005::68

猜你喜欢

转载自www.cnblogs.com/woods1815/p/9904035.html