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!
Coding an Inverse Subnet Mask using PHP
Moderator: General Moderators
Coding an Inverse Subnet Mask using PHP
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!
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
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
I'll try that out. Thanks!
Is there any other method?
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Coding an Inverse Subnet Mask using PHP
Code: Select all
$testValue = '255.255.255.0';
function reverse($a) {
return $a ^ 255;
}
$result = implode('.',array_map('reverse',explode('.',$testValue)));
echo $result;