Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php 中IPV6 ip2long的问题解决办法

php 中IPV6 ip2long的问题解决办法

发布时间:2018-11-19   编辑:www.jquerycn.cn
jquery中文网为您提供php 中IPV6 ip2long的问题解决办法等资源,欢迎您收藏本站,我们将为您提供最新的php 中IPV6 ip2long的问题解决办法资源
在32位系统,ip2long不能转换IPv6,但您可以转换ip2bin和bin2ip 这个函数转换为IPv4和IPv6,返回false,如果是无效的

实例程序

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3365')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3365>

<?php
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_pton($ip)) === false) return false;
    $bits = 15; // 16 x 8 bit = 128bit (ipv6)
    while ($bits >= 0)
    {
        $bin = sprintf("b",(ord($ip_n[$bits])));
        $ipbin = $bin.$ipbin;
        $bits--;
    }
    return $ipbin;
}


function bin2ip($bin)
{
   if(strlen($bin) <= 32) // 32bits (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_pton(substr($ipv6,0,-1)));
}
?>

您可能感兴趣的文章:
php 中IPV6 ip2long的问题解决办法
PHP函数ip2long()实现IP转换成整型时出现负数的解决方法
php解决ipv6使curl获取网页内容超时解决办法
PHP日常实用小Tips
Win8系统磁盘显示被占用100%怎么办?磁盘占100%解决方法
Win7提示设备管理器中出现叹号设备“Teredo tunneling pseudo-interface”
解决php curl获取网页内容提示405 method not allowed状态码
uTorrent卸载不了怎么办 uTorrent卸载不了解决办法
PHP Curl出现403错误怎么办?curl错误解决方法
JDBC中的Communications link failure due to underlying exception错误

[关闭]