Arun kumar K
Arun kumar K

Reputation: 21

How can i increment "id" to infinity and print same value ranges

I have a port $id and $port value to enter in an HTML form. Then, the two inputs will generate a $key.

Here, when I enter $id =0 and $port = (something between 1014 to 65535), it should generate IP range($key) from 1 to 64. it does generate them.

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

The problem is when I enter or dont enter $id, it still generates same $key, this PHP code does not include $Id. I want the $Id to get incremented after 64.

I.e. $id=0 for $portRanges[1024-65535] should generate $key from 0-64

Similarly: $id=1 for $portRanges[1024-65535] should generate $key from 0-64.

The $id should increase infinity here. I tried the above method and it does not work. Any assistance would be really appreciated. Thanks!!

Upvotes: 0

Views: 69

Answers (1)

Jason K
Jason K

Reputation: 1381

I think this is what you are trying to do.

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

Upvotes: 1

Related Questions