TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Advertisement
How to keep your forms from double posting data
   If you find that your users are constantly double submitting the same form, whether it be them hitting the back button, or refresh on the after-post page.

What we are going to do is create a token for each user that is used to identify a particular form use.

Every page that you have a form, you need to set a session variable which will include this token. This token should be generated brand new any time the following conditions are met: The form is not posted OR the form is posted and there are errors.
I personally use an $errors array to keep track of errors, so I know that if count($errors) == 0, I have no errors.

PHP Code:
// Update token any time we have a form that has not been posted too
if (count($_POST) == 0)
{
    
$_SESSION['token'] == md5(time());
}
// We are only going to do the following if we have no form errors
elseif (count($errors) === 0)
{
    
// Check for token
    
if (empty($_SESSION['token']) || $_SESSION['token'] == '')
    {
        die(
'ERROR: Token has not been set');
    }
    
// The two match, so lets go ahead and do our form processing
    // set the token equal to '' so that it can fail on the next attempt at posting
    // of this form
    
elseif ($_SESSION['token'] == $_POST['token'])
    {
        
$_SESSION['token'] = '';
    }
    
// since $_SESSION['token'] is now == '' it will run this last 
    // and end up dieing()
    
else
    {
        die(
'This form data has already been submitted');
    }


Now, in our form, we need to include a hidden value for our token to transverse between the forms.
Should the form be displayed twice (from error handling, remember that because of our code above, our session key will not regenerate itself.
Code:
<form method="post" action="">
    <input type="hidden" name="token" value="<? echo $_SESSION['token']" />
    <input type="text" name="name" id="name" value="" />
</form>
Please note that this is just a basic example in its pretty much simplest form. You have the concept, now put it in action! I did not do any of the form processing or data handling to keep things as clean as possible.
Report this Article
Last 5 Article Reviews Read All Reviews
   .....
Review added by ryanmr on 01-08-2009
If you have page post.php and you post some things to it, and then you subsequently refresh once you're on post.php, will this solution still function properly?
   Fairly simple, yet effective
Review added by ReSpawN on 10-10-2008
Hey mate, this is a pretty fine example of using unregistered posting area's. Normally you would make a flood limit with [10-30] seconds or so, but this'll do fine non the less.

Another option is, to javascript the form. Give the form a name (I personally like the ID better), get the ID with Javascript and instead of making a type="submit" button, make a type="button" with an onclick="" action to your function in Javascript. Give this button also an id (perhaps called id="buttonID") and when clicked, init the function for document.idName.submit() and then disable the button. :)
   Nice article!
Review added by wiifanatic on 09-17-2008
Simple, but it works!
   Nice one
Review added by Jim on 08-27-2008
A quite simple but nice working example, thanks mate!

All times are GMT. The time now is 10:59 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design