php 计算网络地址的bug
迷茫
迷茫 2017-04-10 15:44:18
0
1
776

`function ipToLong($network)
{ $networkLong = '';
if(filter_var($network, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)){
$networkLong=base_convert(sprintf("%u",ip2long($network)), 10, 2);
}else{
$networkPack = inet_pton($network);

$bits = 15; while ($bits >= 0) { $bin = sprintf("%08b", (ord($networkPack[$bits]))); $networkLong = $bin.$networkLong; $bits--; } } return $networkLong;

}
/获取网络地址/
function getSegment($network,$mask){
if(filter_var($network, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)){
$ipv4Long=ipToLong($network);
$maskLong=str_pad(str_repeat(1, $mask), 32, 0);
$segment=$ipv4Long & $maskLong;
return $segment;
}else{
$ipv6Long=ipToLong($network);
$maskLong=str_pad(str_repeat(1, $mask), 128, 0);
$segment=$ipv6Long & $maskLong;
return $segment;
}
}
echo ipToLong('106.32.59.244');
echo '
';
echo getSegment('106.32.59.244','29');
?> `
上面计算出来的网络地址就是106.32.59.244,为什么和下图计算出来的不一致呢?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all (1)
Ty80

因为和你机器是32位还是64位有关系~

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!