Skip to content

Instantly share code, notes, and snippets.

@nerr
Created June 11, 2014 10:11
Show Gist options
  • Save nerr/1afe104295c42d10d302 to your computer and use it in GitHub Desktop.
Save nerr/1afe104295c42d10d302 to your computer and use it in GitHub Desktop.
PHP类ipinfo.class.php,支持64位
<?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;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment