« Posts tagged IP

ipInfo.class.php更新

加入了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;      
    }
}
?>

PHP获取给定IP网段信息

已更新,请移步至《ipInfo.class.php更新》

刚写了段代码, 主要功能是通过给定的网段信息(如10.0.0.1/22), 获取该网段的起始IP地址, 掩码IP地址和广播地址等的信息. 写这段代码的目的是做IP地址查询, 主要用于查询大型集团式企业日志服务器中成百上千个设备中的某一个是归属于哪一个分支企业的(分支企业按一定规则划分了不同的网段). 代码如下:

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php 
/* 
* Leon 
* http://nerrsoft.com * leon@nerrsoft.com 
* 2010-08-11 
*/ 
class ipInfo{ //根据给定的IP字串获取IP信息 
    public function getIpInfo($ipStr){ 
        if(!$this->valid($ipStr)){ 
            return false; 
        } 
        $ipArr = explode('/', $ipStr); 
        //information 
        $info['ipStr'] = $ipStr; 
        $info['bin']['mask'] = $this->getSubnetMask($ipArr[1]); 
        $info['ip']['mask'] = $this->bin2ip($info['bin']['mask']); 
        $info['long']['mask'] = ip2long($info['ip']['mask']); 
        $info['bin']['net'] = $this->ip2bin($ipArr[0]) & $info['bin']['mask']; 
        $info['ip']['net'] = $this->bin2ip($info['bin']['net']); 
        $info['long']['net'] = ip2long($info['ip']['net']); 
        $info['ip']['begin'] = long2ip($info['long']['net']+1); 
        $info['bin']['begin'] = $this->ip2bin($ipArr[0]); 
        $info['long']['begin'] = ip2long($info['ip']['begin']); 
        $info['ip']['end'] = long2ip(abs($info['long']['mask'])+$info['long']['begin']-3); 
        $info['bin']['end'] = $this->ip2bin($info['ip']['end']); 
        $info['long']['end'] = ip2long($info['ip']['end']); 
        $info['ip']['broacast'] = long2ip($info['long']['end']+1); 
        $info['bin']['broacast'] = $this->ip2bin($info['ip']['broacast']); 
        $info['long']['broacast'] = ip2long($info['ip']['broacast']); 
        return $info; 
    } 
    //验证IP字串格式有效性 10.0.0.1/24 
    private function valid($ipStr){ 
        if(preg_match("/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\/\d{1,2}$/", $ipStr)){ 
            return true; 
        }else{ 
            return false; 
        } 
    } 
    //获取二进制 
    private function get_bin($number){ 
        return str_pad(decbin($number),8,'0',STR_PAD_LEFT); 
        return decbin($number); 
    } 
    //IP地址转二进制 
    private function ip2bin($ip){ 
        $ip_octets = split("\.", $ip);
        unset($bin_sn); 
        foreach($ip_octets as $val){ 
            $bin_sn[] = $this->get_bin($val);
        } 
        return join(".", $bin_sn); 
    } 
    //二进制转IP地址 
    private function bin2ip($ip){ 
        $ip_octets = split("\.", $ip); 
        unset($bin_sn); 
        foreach($ip_octets as $val){ 
            $bin_sn[] = bindec($val); 
        } 
        return join(".", $bin_sn); 
    } 
    //获取子网掩码 
    private function getSubnetMask($mask){ 
        for($i=1; $i<=32; $i++){ 
            if($i<=$mask){ 
                $maskStr .= '1'; 
            }else{ 
                $maskStr .= '0'; 
            } 
            if($i%8==0){ 
                $maskStr .= '.'; 
            } 
        } 
        $maskStr = substr($maskStr, 0, -1); 
        return $maskStr; 
    } 
} 
//example 
$ip = new ipInfo(); 
$result = $ip->getIpInfo('10.229.8.10/22'); 
print_r($result); 
?>