PHP获取指定的IP网段信息

发布时间:2020-05-25编辑:脚本学堂
本文分享一例php代码,用于获取指定的IP网段的信息,包括IP地址与进制间的转换操作,有兴趣的朋友参考下。

本节内容:
PHP获取给定IP网段信息,根据给定的IP字串获取IP信息,验证IP字串格式有效性,获取二进制,IP地址转二进制,二进制转IP地址,获取子网掩码。

例子:
 

复制代码 代码示例:
<?php
/**
* PHP获取给定IP网段信息
* by www.jb200.com
*/
    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}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|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('121.207.247.202/26');
    print_r($result);
?>
 

输入结果:
 

Array
(
    [ipStr] => 121.207.247.202/26
    [bin] => Array
        (
            [mask] => 11111111.11111111.11111111.11000000
            [net] => 01111001.11001111.11110111.11000000
            [begin] => 01111001.11001111.11110111.11001010
            [end] => 01111001.11001111.11110111.01111110
            [broacast] => 01111001.11001111.11110111.01111111
        )
 
    [ip] => Array
        (
            [mask] => 255.255.255.192
            [net] => 121.207.247.192
            [begin] => 121.207.247.193
            [end] => 121.207.247.126
            [broacast] => 121.207.247.127
        )
 
    [long] => Array
        (
            [mask] => 4294967232
            [net] => 2043672512
            [begin] => 2043672513
            [end] => 2043672446
            [broacast] => 2043672447
        )
 
)