Coding an Inverse Subnet Mask using PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
0blivious
Forum Newbie
Posts: 2
Joined: Tue Oct 13, 2009 9:01 pm

Coding an Inverse Subnet Mask using PHP

Post 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!
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: Coding an Inverse Subnet Mask using PHP

Post 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>
 
0blivious
Forum Newbie
Posts: 2
Joined: Tue Oct 13, 2009 9:01 pm

Re: Coding an Inverse Subnet Mask using PHP

Post by 0blivious »

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

Post 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;
 
Post Reply