Page 1 of 1

Coding an Inverse Subnet Mask using PHP

Posted: Tue Oct 13, 2009 9:05 pm
by 0blivious
Hi all! :) Im currently developing a webpage with regards to network security and routing. For the security part, im required to have a textbox where users enter in the network subnet mask, which is something like this "255.255.255.0".

My question is, is there a way to make that input appear in PHP code as "0.0.0.255"? I need to subtract 255 from each octet. But i dont know how. Please help!

Re: Coding an Inverse Subnet Mask using PHP

Posted: Tue Oct 13, 2009 9:25 pm
by Weiry
This probably isnt the most efficient way:

Code: Select all

<?php
if(isset($_POST['ipaddress'])){
    $ip = $_POST['ipaddress'];
    $ipArr = explode(".", $ip);
    $invIpArr = array();
 
    foreach($ipArr as $octet){
        array_push($invIpArr, (255-$octet));
    }
    $ipString = $invIpArr[0].".".$invIpArr[1].".".$invIpArr[2].".".$invIpArr[3];
    print $ipString;
}
?>
<form method='POST' action="">
<input type='text' name='ipaddress'/><br/>
<input type='submit' name='ipSub' value='Calculate Inverse'/>
</form>
 

Re: Coding an Inverse Subnet Mask using PHP

Posted: Tue Oct 13, 2009 9:28 pm
by 0blivious
I'll try that out. Thanks! :) Is there any other method?

Re: Coding an Inverse Subnet Mask using PHP

Posted: Wed Oct 14, 2009 3:15 am
by Mark Baker

Code: Select all

 
$testValue = '255.255.255.0';
 
function reverse($a) {
    return $a ^ 255;
}
 
$result = implode('.',array_map('reverse',explode('.',$testValue)));
 
echo $result;