然后,服务器接受IPv6套接字上的连接.某些操作系统可以在IPv6套接字上同时执行IPv4和IPv6.当发生这种情况时,IPv6地址看起来像:: ffff:192.0.2.123或:: ffff:c000:027b,它们是相同的地址但用十六进制编写.
原文链接:https://www.f2er.com/php/133168.html如果您看到像2a00:8640:1 :: 224:36ff:feef:1d89这样的IPv6地址,那么您的网络服务器确实可通过IPv6访问:-)@H_403_3@
无论如何,要将所有内容转换回规范形式,您可以使用以下内容:@H_403_3@
// Known prefix $v4mapped_prefix_hex = '00000000000000000000ffff'; $v4mapped_prefix_bin = pack("H*",$v4mapped_prefix_hex); // Or more readable when using PHP >= 5.4 # $v4mapped_prefix_bin = hex2bin($v4mapped_prefix_hex); // Parse $addr = $_SERVER['REMOTE_ADDR']; $addr_bin = inet_pton($addr); if( $addr_bin === FALSE ) { // Unparsable? How did they connect?!? die('Invalid IP address'); } // Check prefix if( substr($addr_bin,strlen($v4mapped_prefix_bin)) == $v4mapped_prefix_bin) { // Strip prefix $addr_bin = substr($addr_bin,strlen($v4mapped_prefix_bin)); } // Convert back to printable address in canonical form $addr = inet_ntop($addr_bin);
::ffff:192.000.002.123 ::ffff:192.0.2.123 0000:0000:0000:0000:0000:ffff:c000:027b ::ffff:c000:027b ::ffff:c000:27b 192.000.002.123 192.0.2.123
你总是得到规范的IPv4地址192.0.2.123作为输出.@H_403_3@
当然,IPv6地址作为规范的IPv6地址返回:2a00:8640:0001:0000:0224:36ff:feef:1d89变为2a00:8640:1 :: 224:36ff:feef:1d89等@H_403_3@