Preventing Multiple Submissions of A Form
Preventing Multiple Submissions of A Form
One possible occurrence that happens often is that users become impatient when waiting for your
script to do what it is doing, and hence they click the submit button on a form repeatedly. This
can wreak havoc on your script because, while the user may not see anything happening, your
script is probably going ahead with whatever it has been programmed to do. Of particular danger
are credit card number submittals. If a user continually hits the submit button on a credit card
submittal form, their card may be charged multiple times if the developer has not taken the time
to validate against such an eventuality.
For the following example, consider a test.txt text file that you can create and place relative to
the script.
Handling file uploads in PHP is not exactly difficult from a syntax point of view, but it is
important (extremely important in fact) to ensure that the file being uploaded is within the upload
constraints you lay out for it. In other words, an individual user could easily upload a virus or
some other form of malicious software if you are not careful about allowing them to upload only
what you want from them. A similar consideration is file size. You could easily find your server
under some heavy loads if you are not careful about what size of files are being uploaded.
$_FILES Arguments are as follows
As for moving the actual file and saving it, use two methods for performing this action. The two
functions in PHP that will allow to save a file are the copy() and move_uploaded_file()
functions. Use the move_uploaded_file() function, as it will work even when PHP’s safe mode is
enabled. If PHP has its safe mode enabled, the copy() function will fail. They both work largely
the same, so there is no real downside to using the move_uploaded_file() function over the
copy() function.
Below you will find two simple tricks for preventing duplicate submissions, you can use either of
these or a combination of both.
Using Javascript to block duplicate submissions is probably the easiest way. When someone
submits the form we simply disable the Submit button and maybe change it's value to something
more descriptive, like "Submitting, please wait..."
Try clicking this button for example. It will remain disabled until you reload this page:
The first step is to give your submit button a unique id, for example id="myButton":
The second (and last) step is to give two Javascript commands to the <form> tag. The first one
will tell the browser to disable the submit button after the form has been submitted and the
second one will change the button text to give the user some idea about what's happening. This is
the code to add to your form tag:
onsubmit="document.getElementById('myButton').disabled=true;
document.getElementById('myButton').value='Submitting, please wait...';"
That's it. This trick should work in most modern browsers (IE 5+, FireFox, Opera, ...).
If you wish to avoid duplicate submissions for the entire browser session (or longer) you can
consider using cookies. For example edit your form processing script to send a cookie to the
browser after the form has been processed but before any HTML or redirection headers are
printed. Placing this code after the mail() command should work in most cases:
setcookie('FormSubmitted', '1');
Then check for the cookie before processing. If it's there this visitor already submitted the form
in active browser session. Add this code to the beginning of your form processing script:
if (isset($_COOKIE['FormSubmitted']))
{
die('You may only submit this form once per session!');
}
That's it!
<html>
<body>
<form action="myform.php" method="post">
<p>Your Name: <input type="text" name="yourname" /><br />
E-mail: <input type="text" name="email" /></p>
See the example HTML code above? This is a simple HTML form with two input fields, one
radio box group and a text area for comments. Let's say we save this code in a file called
"test.html". When submitted data is sent to the "myform.php" file using POST HTTP method.
All variables passed to the current script via the HTTP POST method are stored in associative
array $_POST. In other words, in PHP you can access data from each field using
$_POST['NAME'], where NAME is the actual field name. If you submit the form above you
would have access to a number of $_POST array values inside the myform.php file:
Now, if you wanted to display submitted data you could simply echo all the variables as shown
below, but do not! Why? Read further.
<html>
<body>
Your name is: <?php echo $_POST['yourname']; ?><br />
Your e-mail: <?php echo $_POST['email']; ?><br />
<br />
Do you like this website? <?php echo $_POST['likeit']; ?><br />
<br />
Comments:<br />
<?php echo $_POST['comments']; ?>
</body>
</html>
If you saved this code in a file called "myform.php", filled the fields in the test.html form and hit
the Submit button, the myform.php output would look something like this:
Quite simple, isn't it? But the most important thing is still missing! You need to validate
submitted data to protect your script (and thus your website and server) from malicious code.
Let's say you display all data submitted with the form in a HTML file (like a guestbook does for
example). Now consider someone types this code instead of his name:
<script>location.href('http://www.SPAM.com')</script>
If this is stored in a HTML file anyone who tried to view it would be redirected to
http://www.SPAM.com! And this is the least that can happen! Failure to properly validate input
data is the main reason for most vulnerabilities and exploits in PHP scripts. You wouldn't want
someone to hack your website, erase all data and upload his/her own "u \/\/3R3 H4><0r3d!"
homepage, would you?
Read this tutorial further to learn how to validate form inputs and protect yourself from exploits.
<?php
/* Prevent duplicate submissions */
if (isset($_COOKIE['FormSubmitted']))
{
show_error('You may only submit this form once per session!');
}
Name: $yourname
E-mail: $email
URL: $website
Comments:
$comments
End of message
";
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
</body>
</html>
<?php
exit();
}
?>