Prevent Multiple Form Submission in PHP

0
Your rating: None

To prevent multiple form submission just generate a unique identifier and store the token as a hidden field in the form. Immediately before form processing, check to see if that token has already been submitted. If it hasn't, you can proceed; if it has, you should generate an error.

While creating the form, use uniqid( ) to get a unique identifier:

<?php
$unique_id = uniqid(microtime(),1);
...
?>

Then, when processing, look for this ID:

$unique_id = $dbh->quote($_GET['unique_id']);
$sth = $dbh->query("SELECT * FROM database WHERE unique_id = $unique_id");
if ($sth->numRows( )) {
    // already submitted, throw an error
} else {
   // act upon the data
}

For numerous reason, users may resubmit a form. Usually it may be a slip-of-the-mouse: double-clicking the Submit button. They may hit their web browser's Back button to edit or recheck
information, but then they re-hit Submit instead of Forward.

So inorder to prevent database from being cluttered duplicate records. By generating a token that's placed in the form, you will uniquely identify that specific instance of the form even if the cookies is disabled. While saveing the form's
data, just store the token alongside it which will allows you to easily check if you already have this form and record the database it belongs to.

Start by adding an extra column to your database table — unique_id — to hold the identifier. When you insert data for a record, add the ID also. By associating the exact row in the database with the form, you can more easily handle a
resubmission.

 #

The interesting moment

 
 #

an easier way would be to use a cookie or session. that'll prevent having to store extra data that is not needed into a database.

 

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.

Cumulus Tag Cloud

Kiran Says

SEO Architect,SEO Specialist reflect my values.

By enduring black hat methods the SEO industry is setting itself up for washout and sleepwalking into oblivion.

Jobs in India