经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
PHP中有关IPV4 和IPV6地址转换问题
来源:cnblogs  作者:且听の风吟  时间:2018/11/5 11:09:23  对本文有异议

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

  • 首先介绍两个函数:

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

  1. int ip2long ( string $ip_address )

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

  1. 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 出现负数问题。

示例:

  1. $ip_long = ip2long('192.168.8.30');$long_ip = long2ip($ip_long);echo $ip_long;    // -1062729698echo $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地址。

解决办法:

  1. $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 限制。

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

  1. /**
  2.  * 代替 long2ip 函数
  3.  * @param $ip
  4.  * @return string */function convertIpToString($ip)
  5. {$long = 4294967295 - ($ip - 1);return long2ip(-$long);
  6. }/**
  7.  * 代替 ip2long 函数
  8.  * @param $ip
  9.  * @return string */function convertIpToLong($ip)
  10. {return sprintf("%u", ip2long($ip));
  11. }

测试调用:

  1. $ip_long = $this->convertIpToLong('192.168.8.30');$long_ip = $this->convertIpToString($ip_long);echo $ip_long; // 3232237598echo $long_ip; // 192.168.8.30

 

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

  • MySQL 中提供了几个函数,INET_ATON 将 IPV4 地址转换为整数。 INET_NTOA 将整数转换为 IPV4 地址。

  • 如果是 IPV6 地址也有对应的方法:INET6_ATON 和 INET6_NTOA,这两个方法需要 5.6 以上版本才能使用。

调用示例:

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

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

  • PHP 中没有直接提供函数实现 IPV6 地址的转换。 不过PHP手册中提供了两个方法可以实现这一需求。要运行这两个方法首先需要开启 php_gmp.dll 模块。

  1. /**
  2.  * IPV6 地址转换为整数
  3.  * @param $ipv6
  4.  * @return string */function ip2long6($ipv6)
  5. {$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--;
  6.     }return gmp_strval(gmp_init($ipv6long, 2), 10);
  7. }/**
  8.  * 整数转换为 IPV6 地址
  9.  * @param $ipv6long
  10.  * @return string */function long2ip6($ipv6long)
  11. {$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;
  12.         }
  13.     }$bits = 0;$ipv6 = '';while ($bits <= 7) {$bin_part = substr($bin, ($bits * 16), 16);$ipv6 .= dechex(bindec($bin_part)) . ":";$bits++;
  14.     }// compressreturn inet_ntop(inet_pton(substr($ipv6, 0, -1)));
  15. }

测试调用:

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

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号