Im quite new to PHP, i was wondering what methods/preventions other programmers use to stop data being entered twice into a MySQL database when a user refreshes on the same page as a form? Obviouly it happens and i need a good way to stop this.
Thanks, Ben
You can use a token to prevent the page from being processed again! Such a procedure is used in a lot web frameworks !
The pattern you should use is the "Synchronizer Token Pattern"! If you have a serviceoriented application, you can save your status in the Database.
The data can be send via JavaScript or by a hidden form field.
You should also have a look at libaries with out of the box support for things like this! Grails is such one!
See: http://www.grails.org/1.1-Beta3+Release+Notes ...
...
..
Ilya's answer is correct, I just wanted to add a little more than would fit in a comment:
If resubmission is dangerous (going back and submitting again, reloading the result page [if you haven't taken Ilya's advice], etc.) I use a "nonce" to make sure the form can only go through once.
On the form page:
In the processing page:
After the form has been submitted once, it cannot be submitted again, unless the user intentionally fills it out a second time.
Try including something in your forms to prevent double-submission, preferably at the same time protecting against cross-site request forgery. I recommend using what I call a formkey, which is a one-use field that uniquely identifies a form submission, tying it to an individual user at an individual address. The concept goes under other names too, but the short note I've linked to explains it well enough.
POE (Post Once Exactly) is an HTTP pattern aimed at warning the client to block double submits using a proprietary header ...
... but is still in specification.
http://www.mnot.net/drafts/draft-nottingham-http-poe-00.txt
I think the above nonce is a good solution. Though storing the nonce as a discrete session variable will introduce some errors if the client is attempting to perform simultaneous posts from multiple tabs. Maybe better to ...
... and ...
... to allow for multiple nonces (nonci? noncei?).
My two cents:
if(isset($_POST['submit'])
Other useful information for similar cases:
Give a look to LOCK TABLES in MySQL
Disable buttons via JavaScript after the first click
You might want to check out the POST/Redirect/GET pattern most modern web apps implement, see http://en.wikipedia.org/wiki/Post/Redirect/Get