PHP获取给定IP网段信息

PHP获取给定IP网段信息,根据给定的IP字串获取IP信息,验证IP字串格式有效性,获取二进制,IP地址转二进制,二进制转IP地址,获取子网掩码。不多说上代码。
<?php
    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('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
        )

)

转载于:https://my.oschina.net/766/blog/210940

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值