Domain Discussion Board

Go Back   Domain Discussion Board > General > Tutorials

 
Reply
 
LinkBack Thread Tools Display Modes
Guarding Against Multiple Submission of the Same Form
Old
  (#1 (permalink))
lucifer00
Executive Member
lucifer00 is an unknown quantity at this point
 
Status: Offline
Posts: 342
Points: 50.4
Bank: 2,403.5
Total Points: 2,453.9
Donate
Join Date: Sep 2007
Rep Power: 0
Guarding Against Multiple Submission of the Same Form - 09-08-2007, 12:44 PM

this is in PHP...

generate a unique identifier and store the token as a hidden field in the form. Before
processing the form, 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.
When creating the form, use uniqid( ) to get a unique identifier:
<?php
$unique_id = uniqid(microtime(),1);
...
?>
<input type="hidden" name="unique_id" value="<?php echo $unique_id; ?>">
</form>
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 a variety of reasons, users often resubmit a form. Usually it's a slip-of-the-mouse: doubleclicking
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. It can be intentional: they're
trying to stuff the ballot box for an online survey or sweepstakes. Our Solution prevents the
nonmalicious attack and can slow down the malicious user. It won't, however, eliminate all
fraudulent use: more complicated work is required for that.
The Solution does prevent your database from being cluttered with too many copies of the
same record. By generating a token that's placed in the form, you can uniquely identify that
specific instance of the form, even when cookies is disabled. When you then save the form's
data, you store the token alongside it. That allows you to easily check if you've already seen
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. For example:
$username = $dbh->quote($_GET['username']);
$unique_id = $dbh->quote($_GET['unique_id']);
$sth = $dbh->query("INSERT INTO members ( username, unique_id)
VALUES ($username, $unique_id)");
By associating the exact row in the database with the form, you can more easily handle a
resubmission. There's no correct answer here; it depends on your situation. In some cases,
you'll want to ignore the second posting all together. In others, you'll want to check if the
record has changed, and, if so, present the user with a dialog box asking if they want to
update the record with the new information or keep the old data. Finally, to reflect the second
form submission, you could update the record silently, and the user never learns of a problem.
All these possibilities should be considered given the specifics of the interaction. Our opinion is
there's no reason to allow the deficits of HTTP to dictate the user experience. So, while the
third choice, silently updating the record, isn't what normally happens, in many ways this is
the most natural option. Applications we've developed with this method are more user
friendly; the other two methods confuse or frustrate most users.
It's tempting to avoid generating a random token and instead use a number one greater then
the number of records already in the database. The token and the primary key will thus be the
same, and you don't need to use an extra column. There are (at least) two problems with this
method. First, it creates a race condition. What happens when a second person starts the form
before the first person has completed it? The second form will then have the same token as
the first, and conflicts will occur. This can be worked around by creating a new blank record in
the database when the form is requested, so the second person will get a number one higher
than the first. However, this can lead to empty rows in the database if users opt not to
complete the form.
The other reason not do this is because it makes it trivial to edit another record in the
database by manually adjusting the ID to a different number. Depending on your security
settings, a fake GET or POST submission allows the data to be altered without difficulty. A long
random token, however, can't be guessed merely by moving to a different integer.

O'Reilly php Cook Book

Last edited by lucifer00 : 09-09-2007 at 10:08 AM.
   
Reply With Quote
Re: Guarding Against Multiple Submission of the Same Form
Old
  (#2 (permalink))
lplover2k
Member
lplover2k is on a distinguished road
 
Status: Offline
Posts: 50
Points: 415.4
Bank: 2,811.8
Total Points: 3,227.2
Donate
Join Date: Aug 2007
Rep Power: 2
Re: Guarding Against Multiple Submission of the Same Form - 09-08-2007, 12:54 PM

what a coincidence..was searching for a tuto on that for my registration form of my website ..thx
   
Reply With Quote
Sponsored Links
Re: Guarding Against Multiple Submission of the Same Form
Old
  (#3 (permalink))
lucifer00
Executive Member
lucifer00 is an unknown quantity at this point
 
Status: Offline
Posts: 342
Points: 50.4
Bank: 2,403.5
Total Points: 2,453.9
Donate
Join Date: Sep 2007
Rep Power: 0
Re: Guarding Against Multiple Submission of the Same Form - 09-09-2007, 10:08 AM

sorry i forgot to mention the source of the above Thread is
O 'Reilly PHP Cook BOOK


------------------------------------
Find the Best Deals at:
AUCSAL.COM
------------------------------------
   
Reply With Quote
Re: Guarding Against Multiple Submission of the Same Form
Old
  (#4 (permalink))
lplover2k
Member
lplover2k is on a distinguished road
 
Status: Offline
Posts: 50
Points: 415.4
Bank: 2,811.8
Total Points: 3,227.2
Donate
Join Date: Aug 2007
Rep Power: 2
Re: Guarding Against Multiple Submission of the Same Form - 09-11-2007, 08:47 PM

ok what i do ..is to redirect the user to another page once the user has all his details inserted in the database... it seems to work too
   
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Points Per Thread View: 1.0
Points Per Thread: 15.0
Points Per Reply: 5.0


Similar Threads
Thread Thread Starter Forum Replies Last Post
FREE CGI Hosting blue-sky Wonderful Websites 21 04-27-2008 07:12 AM


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 ©2007, Crawlability, Inc.