kaiman Posted June 19, 2011 Share Posted June 19, 2011 Hi Everyone, I am currently working on a IP Subnet Calculator. The script seems to work okay after I enter information in the form, but I am getting the following error when I first bring up the page: Fatal error: Unsupported operand types in ... on line 71 The line in question and the full code is below. Any help is appreciated. Thanks, kaiman $broadcast = $network | (~$subnet); <?php // calculates information for given ip address and subnet // default octet number $octet1=0; $octet2=0; $octet3=0; $octet4=0; ?> <form name="Subnet Calculator" form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Enter ip address: <input type="text" name="octet1" value="<?php echo $octet1; ?>" size="2" maxlength="3"> . <input type="text" name="octet2" value="<?php echo $octet2; ?>" size="2" maxlength="3"> . <input type="text" name="octet3" value="<?php echo $octet3; ?>" size="2" maxlength="3"> . <input type="text" name="octet4" value="<?php echo $octet4; ?>" size="2" maxlength="3"><br/> Subnet mask: <select class="select" name="submask" id="submask" onChange="javascript:enableOther();"> <option value="">Please select an option:</option> <option value="128.0.0.0">128.0.0.0</option> <option value="192.0.0.0">192.0.0.0</option> <option value="224.0.0.0">224.0.0.0</option> <option value="240.0.0.0">240.0.0.0</option> <option value="248.0.0.0">248.0.0.0</option> <option value="252.0.0.0">252.0.0.0</option> <option value="254.0.0.0">254.0.0.0</option> <option value="255.0.0.0">255.0.0.0</option> <option value="255.128.0.0">255.128.0.0</option> <option value="255.192.0.0">255.192.0.0</option> <option value="255.224.0.0">255.224.0.0</option> <option value="255.240.0.0">255.240.0.0</option> <option value="255.248.0.0">255.248.0.0</option> <option value="255.252.0.0">255.252.0.0</option> <option value="255.254.0.0">255.254.0.0</option> <option value="255.255.0.0">255.255.0.0</option> <option value="255.255.128.0">255.255.128.0</option> <option value="255.255.192.0">255.255.192.0</option> <option value="255.255.224.0">255.255.224.0</option> <option value="255.255.240.0">255.255.240.0</option> <option value="255.255.248.0">255.255.248.0</option> <option value="255.255.252.0">255.255.252.0</option> <option value="255.255.254.0">255.255.254.0</option> <option value="255.255.255.0">255.255.255.0</option> <option value="255.255.255.128">255.255.255.128</option> <option value="255.255.255.192">255.255.255.192</option> <option value="255.255.255.224">255.255.255.224</option> <option value="255.255.255.240">255.255.255.240</option> <option value="255.255.255.248">255.255.255.248</option> <option value="255.255.255.252">255.255.255.252</option> <option value="255.255.255.254">255.255.255.254</option> <option value="255.255.255.255">255.255.255.255</option> </select><br/> <input type="submit" name="submit" value="Submit"> </form> <?php // get form data $octet1 = $_POST['octet1'] ; $octet2 = $_POST['octet2'] ; $octet3 = $_POST['octet3'] ; $octet4 = $_POST['octet4'] ; // concantenate ip address $ip_address = $octet1 . "." . $octet2 . "." . $octet3 . "." . $octet4; $subnet_mask = $_POST['submask']; $ip = ip2long($ip_address); $subnet = ip2long($subnet_mask); $network = ($ip & $subnet); $broadcast = $network | (~$subnet); echo "IP Address: " . long2ip($ip) . "\n"; echo "Subnet Mask: " . long2ip($subnet) . "\n"; echo "Network Address: " . long2ip($network) . "\n"; echo "Broadcast Address: " . long2ip($broadcast) . "\n"; echo "Number of Hosts: " . ($broadcast - $network - 1) . "\n"; echo "Host Range: " . long2ip($network + 1) . " - " . long2ip($broadcast - 1) . "\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/239775-help-with-fatal-error/ Share on other sites More sharing options...
AbraCadaver Posted June 19, 2011 Share Posted June 19, 2011 Because all of those values are empty when you first bring up the form. They only have values if you fill them out and submit the form. You should use something like this around your calculation code: if(isset($_POST['submit'])) { // form was submitted, do stuff } And then you should check all inputs that are mandatory with something like this: if(empty($_POST['something'])) { // error something was left blank, but it is required } Quote Link to comment https://forums.phpfreaks.com/topic/239775-help-with-fatal-error/#findComment-1231754 Share on other sites More sharing options...
boompa Posted June 19, 2011 Share Posted June 19, 2011 Something like this will shorten the code: for($i = 1; $i <=4; ++$i) { if (!isset($_POST['octet'.$i])) { // Deal with missing info as desired } } Might not be a bad idea to validate that all data is entered via JavaScript before allowing submission. Quote Link to comment https://forums.phpfreaks.com/topic/239775-help-with-fatal-error/#findComment-1231758 Share on other sites More sharing options...
kaiman Posted June 19, 2011 Author Share Posted June 19, 2011 @boompa and AbraCadaver Thanks for your comments. After I posted this I added: if(isset($_POST['submit'])) { // form was submitted, do stuff } to process the form, however that didn't solve the fatal error issue. To solve the error I added the following if statement which seems to work: if ($network != "" && $subnet != "") $broadcast = $network | (~$subnet); I will be adding some form validation to make this script more secure, but thanks for the other comments. kaiman Quote Link to comment https://forums.phpfreaks.com/topic/239775-help-with-fatal-error/#findComment-1231796 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.