给定IP地址和子网,如何使用php计算IP范围

时间:2013-09-11 05:27:18

标签: php ip-address subnet

我有以下代码片段,我试图检测IP地址是否属于某个范围。大多数时候我拥有IP地址和子网,但有时我只拥有IP地址。

<?php

function CalculateRange($IP,$Subnet=""){

    //Calculate subnetmax
    if ($Subnet==""){

    }        

    //Calculate max IP

    return $MaxIP;
}

//--- IP range
$IPRange = array (
    array("address"=>"196.201.26.0","subnet"=>"255.255.252.0"),
    array("address"=>"196.202.43.0","subnet"=>"255.255.0.0"),
    array("address"=>"196.203.44.0","subnet"=>"255.255.128.0"),
);

//Display MaxIP for each IP and Subnet
foreach ($IPRange as $pair) {
    echo "<p>";
    echo "For IP:{$pair['address']} with Subnet:{$pair['subnet']}.";
    echo "MaxIP is ";
    echo CalculateRange($pair['address'],$pair['subnet']);  
    echo "</p>";
}

?>

我的问题是如何计算IP和子网组合的MaxIP?

2 个答案:

答案 0 :(得分:1)

<?php

$subnet = "255.255.252.0";  // provide your subnet here
$ip = "191.168.31.0";   // provide your ip here
$ip_blocks = explode(".", $ip);
$mask_blocks = explode('.', $subnet);

$ip_class = '';
if($ip_blocks[0] > 0 && $ip_blocks[0] <= 126) {
    $ip_class = 'a';
} elseif($ip_blocks[0] >= 128 && $ip_blocks[0] <= 191) {
    $ip_class = 'b';
} elseif($ip_blocks[0] >= 192 && $ip_blocks[0] <= 223) {
    $ip_class = 'c';
} elseif($ip_blocks[0] >= 224 && $ip_blocks[0] <= 239) {
    $ip_class = 'd';
} elseif($ip_blocks[0] >= 240 && $ip_blocks[0] <= 255) {
    $ip_class = 'e';
} else {
    die('wrong ip');
}

$subnet_class= '';
if($mask_blocks[0]=='255' && $mask_blocks[1] < '255') {
    $subnet_class = 'a';
} elseif($mask_blocks[0]=='255' && $mask_blocks[1]=='255' && $mask_blocks[2] < '255') {
    $subnet_class = 'b';
} else {
    $subnet_class = 'c';
}

echo 'subnet class: '.$subnet_class.'<br />';

$min_ip = '';
$max_ip = '';

if($subnet_class=='b') {
    if($ip_class == $subnet_class) {
        $min_ip = "$ip_blocks[0].$ip_blocks[1].1";
        $max_ip = "$ip_blocks[0].$ip_blocks[1].255";
        echo 'minimum: '.$min_ip.' - maximum: '.$max_ip;
    } else {
        echo 'Error! IP does not lie in this range. ';
        exit;   
    }   
}
// you can continue for other subnet masks as well.. as this is only for subnet for class b

?>

答案 1 :(得分:0)

以下工作完美

function isInRange() {

    //--- IP range
    $IPRange = array (
        array("address"=>"197.207.35.238","subnet"=>"255.255.0.0"),
        array("address"=>"41.207.44.232","subnet"=>"255.255.10.0"),
        array("address"=>"40.207.44.250","subnet"=>"255.255.0.0")
    );

    foreach ($IPRange as $pair) {

        //Check if we have subnet mask
        if ($pair['subnet']!='') {
            // simple example
            $bcast = ip2long($_SERVER['REMOTE_ADDR']);
            $smask = ip2long($pair['subnet']);
            $nmask = $bcast & $smask;
            $SourceStartIP = long2ip($nmask);

            if($SourceStartIP==$pair['address']) {
                //This is in range
                return true;
            }

        } else {

            //--- The header matches something in the fixed list
            if ($pair['address'] == $_SERVER['REMOTE_ADDR']) {

                return true;

            }

        }
    }

    return false;

}