我如何将“ id”增加到无穷大并打印相同的值范围

时间:2019-12-03 16:19:09

标签: php

我有一个端口$id$port值,可以HTML格式输入。然后,两个输入将生成一个$key

在这里,当我输入$id =0$port = (something between 1014 to 65535)时,它应该生成1到64之间的IP range($key)。它确实会生成它们。

<div id="port" align="center">
    <form  action="NetRules.php" method="GET">
        <input type="number" name="id" value="<?php echo $id ?>" placeholder="enter real IP">
        <br/>
        <input type="number" name="port" value="<?php echo $port ?>" placeholder="enter port ID">
        <br/>
        <input type="submit" name="submit" value="Suche">       
    </form>
</div>
<?php

$portRanges = [1024, 2031, 3039, 4047,5055,6063,7071,8079,9087,10095,
               11103,12111,13119,14127,15135,16143,17151,18159,19167,
               20175,21183,22191,23199,24207,25215,26223,27231,28239,
               29247,30255,31263,32272,33279,34287,35295,36303,37311,
               38319,39327,40335,41343,42351,43359,44367,45375,46383,
               47391,48399,49407,50415,51423,52431,53439,54447,55455,
               56463,57471,58479,59497,60945,61503,62511,63519,64527,
               65535];

if(isset($_GET['submit']) && isset($_GET['port'])) {
    $port = $_GET['port'];
    $id = $_GET['id'];
    if($id == 0){
    foreach($portRanges as $key => $value) {
        if(array_key_exists($key+1, $portRanges) && $port >= $value && $port < $portRanges[$key+1]+1) {

            break;
        }
    }
    $id++;
    }
}
?>
<!--Here We Fetch The IP Range-->
<div align="center">
    <form id="content1">
        <h3>Private IP Address:</h3>
        <input type="number" name="IP range" value="<?php echo $key ?>"> 
    </form>
</div>

问题是当我输入或不输入$id时,它仍然生成相同的$key,此PHP代码不包含$Id。我希望$Id在64之后递增。

$id=0的{​​{1}}应该从0-64生成$portRanges[1024-65535]

类似地:$key的{​​{1}}应该从0-64生成$id=1

$portRanges[1024-65535]在此处应增加无穷大。 我尝试了上述方法,但它不起作用。 任何帮助将不胜感激。 谢谢!

1 个答案:

答案 0 :(得分:1)

我认为这就是您想要做的。

<?php

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL);

$portRanges = [1024,2031,3039,4047,5055,6063,7071,8079,9087,10095,
               11103,12111,13119,14127,15135,16143,17151,18159,19167,
               20175,21183,22191,23199,24207,25215,26223,27231,28239,
               29247,30255,31263,32272,33279,34287,35295,36303,37311,
               38319,39327,40335,41343,42351,43359,44367,45375,46383,
               47391,48399,49407,50415,51423,52431,53439,54447,55455,
               56463,57471,58479,59497,60945,61503,62511,63519,64527,
               65535];

$id = isset($_GET['id']) ? $_GET['id'] : 0;
$port = isset($_GET['port']) ? $_GET['port'] : 1024;
if($id < 0 or $id > 255){
    echo 'ID out of range. 0-255<br>';
    $id = 0;
}
if(array_search($port, $portRanges) === False){
    echo 'Invalid Port.<br>';
    $port = 1024;
}

$select = '<SELECT name="port" >';
foreach($portRanges as $key => $value) {
    if($port == $value){
        $opt = ' SELECTED ';
    } else {
        $opt = '';
    }
    $select .= "<option $opt >$value</option>";
}
$select .= '</SELECT>';
$form ='
<div id="port" align="center">
    <form  method="GET">
        <input type="number" name="id" value="'.$id.'" placeholder="enter real IP">
        <br/>
        '.$select.'
        <br/>
        <input type="submit" name="submit" value="Suche">       
    </form>
</div>

<div align="center">
    <form id="content1">
        <h3>Private IP Address:</h3>
        <input type="number" name="IP range" value="'.array_search($port, $portRanges).'"> 
    </form>
</div>
';

echo $form;