Self Processing form - need help cleaning up
Posted: Tue Jul 22, 2008 10:51 am
I have edited your post to add [ php ] and [ /php ] tags to make your script more readable.
I've got a situation wherein I want users, a select few w/ access to the site, to be able to fill out a form that will get posted to a flat file that postfix reads from as an allowed relay list. The code I've copied below works, however it's in need of the finer elements to be polished up and I'm not sure how to do it.
User fills in the following:
Location
Description
IP address
Posters Name
The script checks for blank fields, valid ip, duplicate ip and finally writes the info to the allowed_list file, and eventually will restart postfix (I hope).
My problem, as far as aesthetics are concerned at this point, is that when the data all checks out it prints what was written to file as well as the empty form below the written data. How do I only echo/print the confirmed data? My initial thoughts were to have the form on one page and then hand off the process after all fields are confirmed not empty. But then I had the problem of validating the IP. If the validation failed I couldn't redisplay the form again after clicking submit so I wound up recreating my dilemma all over again if I separated the form from the process.
This is my first attempt at php and I've hack, copied googled, and struggled to get this far so I'd appreciate the help.
code to follow:
I've got a situation wherein I want users, a select few w/ access to the site, to be able to fill out a form that will get posted to a flat file that postfix reads from as an allowed relay list. The code I've copied below works, however it's in need of the finer elements to be polished up and I'm not sure how to do it.
User fills in the following:
Location
Description
IP address
Posters Name
The script checks for blank fields, valid ip, duplicate ip and finally writes the info to the allowed_list file, and eventually will restart postfix (I hope).
My problem, as far as aesthetics are concerned at this point, is that when the data all checks out it prints what was written to file as well as the empty form below the written data. How do I only echo/print the confirmed data? My initial thoughts were to have the form on one page and then hand off the process after all fields are confirmed not empty. But then I had the problem of validating the IP. If the validation failed I couldn't redisplay the form again after clicking submit so I wound up recreating my dilemma all over again if I separated the form from the process.
This is my first attempt at php and I've hack, copied googled, and struggled to get this far so I'd appreciate the help.
code to follow:
Code: Select all
<?php
//define variables
$location = $_POST['location'];
$description = $_POST['description'];
$ip = $_POST['ip'];
$user_name = $_POST['user_name'];
$tried = ($_POST['tried'] == 'yes');
$allowed_list = "/var/www/test/testfile.txt";
clearstatcache ();
//test for empty fields
if ($tried)
{
$validated = (!empty($location) && !empty($description) && !empty($ip) && !empty($user_name));
if (!$validated)
{
?>
<p>
The Location, Description, IP and User Name are <strong>required</strong> fields. Please complete them to proceed.
</p>
<?php
}
}
if ($tried && $validated) {
//test for valid IP
if (preg_match("{^\b((25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)\.){3}(25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)\b$}",$ip))
{ //valid ip = true
echo "You have requested the following data to be added to the Mail Relay file:<br /><br />";
echo "<strong>$location</strong><br />";
echo "<strong>$description</strong><br />";
echo "<strong>$user_name</strong><br /";
echo "<strong>$ip</strong><br /><br />";
echo "Click <a href = \"index.php\">here</a> to go back and enter another item into the relay.<br />";
if (file_exists($allowed_list) == false)
{
die('It seems as tho the file youre attempting to write to doesn\'t exist! Please contact the Systems Engineer ASAP!');
//this isnt dieing, it's still displaying the data that would have been written to file, need to fix this
}
else
// Read file
$data = file_get_contents($allowed_list);
if (preg_match("/$ip/", $data, $array))
{
echo "However the ip address ". $ip . " already exists<br />";
echo "Click <a href = \"javascript:history.go(-1)\">here</a> to go back and re-enter the IP.";
}
else
{
//open relay file for writing
$fh = fopen($allowed_list, 'a+') or die("Can't open file");
fwrite($fh, "#$location\r\n"); //the "#" adds a comment to the first of the newly written line, the "\r\n" adds a carriage $
fwrite($fh, "#$description\r\n");
fwrite($fh, "#$user_name\r\n");
fwrite($fh, "#$date\r\n");
fwrite($fh, "$ip\r\n");
fwrite($fh, "\r\n");
fwrite($fh, "\r\n");
fclose($fh);
}
}
else
{ //valid ip = false
echo "The IP address you entered " . $ip . " appears to be invalid, please re-enter the IP address.<br /><br />";
echo "Click <a href = \"javascript:history.go(-1)\">here</a> to go back and re-enter the IP.";
}
} else {
echo "Please fill the fields below to add an item to the WSC MailRelay.<br /><br />"; echo "<strong>ALL FIELDS ARE REQUIRED</strong><br /><br />";
}
?>
<form action="<?= $PHP_SELF ?>" method="POST">
Location: <input type=text name="location" value="<?= $location ?>" /><br /> <br />
Description: <input type=text name="description" value="<?= $description ?>" /><br /><br />
IP: <input type=integer name="ip" value="<?= $ip ?>" /><br /><br />
Your Name: <input type=text name="user_name" value="<?= $user_name ?>" /><br /> <br />
<input type="hidden" name="date" value="<?php print(date("m/d/Y, h:s A")); ?>">
<input type="hidden" name="tried" value="yes" />
<input type="submit" value="<?php echo $tried ? 'Write to file' : 'Create'; ?>" />
</form>