How to stop Postback form submissions in PHP

When I was new to Programming and was not so familiar with Form Post backs it was a headache for me because Sometimes user may hit submit button twice or user may reload the page(with post data), This both activities make form processing multiple times with same data,this results duplicate records in DB or duplicate Emails in the Inbox.

To escape this behavior in PHP I started to include a unique token on each post request. this method is also useful to prevent CSRF and replay attacks. I generated unique key in hidden filed ( <input type=”hidden” /> ) in form and also in session variable. Once the form is submitted i compared hidden field value with session token value, if both values matched then the form submission is valid ,if token does not match the token in our session , the form has been re-submitted.

So now below you can check my code :

PHP Code

Create a hidden Field

Complete Example

Hope it will Help You and please like my facebook page :

and make me proud.By the way if you face any problem then let me know in comment section below

Leave a Reply

Your email address will not be published. Required fields are marked *