簡體   English   中英

給定一個IP地址和一個子網,我如何使用php計算IP范圍

[英]Given an IP address and a Subnet, how do I calculate the range of IPs using php

我有以下代碼片段,我正在嘗試檢測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?

<?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

?>

下面完美地工作

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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM