Solution to the issue of IPV6ip2long in php. In a 32-bit system, ip2long cannot be converted to IPv6, but you can convert ip2bin and bin2ip to IPv4 and IPv6, and false is returned, if the instance program code is invalid, copy the code below in a 32-bit system, ip2long cannot be converted to IPv6, but you can convert ip2bin and bin2ip to IPv4 and IPv6, and return false, invalid
Instance program
The code is as follows: |
|
Function ip2bin ($ ip) { If (filter_var ($ ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 )! = False) Return base_convert (ip2long ($ ip), 10, 2 ); If (filter_var ($ ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) = false) Return false; If ($ ip_n = inet_ton ($ ip) === false) return false; $ Bits = 15; // 16x8 bit = 128bit (ipv6) While ($ bits> = 0) { $ Bin = sprintf ("% 08b", (ord ($ ip_n [$ bits]); $ Ipbin = $ bin. $ ipbin; $ Bits --; } Return $ ipbin; } Function bin2ip ($ bin) { If (strlen ($ bin) <= 32) // 32 bits (ipv4) Return long2ip (base_convert ($ bin, 2, 10 )); If (strlen ($ bin )! = 128) Return false; $ Pad = 128-strlen ($ bin ); For ($ I = 1; $ I <= $ pad; $ I ++) { $ Bin = "0". $ bin; } $ Bits = 0; While ($ bits <= 7) { $ Bin_part = substr ($ bin, ($ bits * 16), 16 ); $ Ipv6. = dechex (bindec ($ bin_part )).":"; $ Bits ++; } Return inet_ntop (inet_ton (substr ($ ipv6, 0,-1 ))); } ?>
|
The program code of the producer instance is as follows...