加入了64位与32位环境的判断(ip2long),更新了计算方法。
PHP
<?php /* * Leon * http://nerrsoft.com * leon@nerrsoft.com * 2011-04-11 */ class ipInfo{ var $address; var $netbits; public function getIpInfo($ipStr){ list($this->address, $this->netbits) = explode('/', $ipStr); $info['long']['mask'] = $this->netmask(); $info['ip']['mask'] = long2ip($info['long']['mask']); $info['long']['net'] = $this->network(); $info['ip']['net'] = long2ip($info['long']['net']); $info['long']['begin'] = $info['long']['net'] + 1; $info['ip']['begin'] = long2ip($info['long']['begin']); $info['long']['end'] = abs($info['long']['mask']) + $info['long']['begin'] - 3; $info['ip']['end'] = long2ip($info['long']['end']); $info['long']['broacast'] = $this->broadcast(); $info['ip']['broacast'] = long2ip($info['long']['broacast']); $info['count'] = $info['long']['broacast'] - $info['long']['net'] - 1; return $info; } // Return the netmask function netmask(){ $mask = $this->check6432('255.255.255.255'); return $mask << (32 - $this->netbits); } // Return the network that the address sits in function network(){ $ip = $this->check6432($this->address); return ($ip & $this->netmask()); } // Return the broadcast that the address sits in function broadcast(){ return ($this->network() | (~$this->netmask())); } // Return the inverse mask of the netmask function inverse(){ $mask = $this->check6432('255.255.255.255'); return (long2ip(~($mask << (32 - $this->netbits)))); } // function check6432($ip){ if(PHP_INT_MAX==2147483647){ $mask = ip2long($ip); }else{ list(, $mask) = unpack('l', pack('l', ip2long($ip))); } return $mask; } } ?>
Comments