php 计算网络地址的bug

WBOY
Release: 2016-06-06 20:32:29
Original
1127 people have browsed it

` 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);

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

}
/获取网络地址/
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,为什么和下图计算出来的不一致呢?

php 计算网络地址的bug

回复内容:

` 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);

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

}
/获取网络地址/
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,为什么和下图计算出来的不一致呢?

php 计算网络地址的bug

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

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template