Forum Moderators: coopster

Message Too Old, No Replies

How to prevent form being submitted twice

Not sure how to address it

         

henry0

7:18 pm on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a form that uses
$username=$_POST['username'];
etc...
while testing
I found that if an user directed away from the form through header ("location.....
disregards its actual location and use its back browser
capability back and forth it will then resend the form

How should I manage to avoid a form to be sent twice
thanks
Henry

ExpLarry

7:49 pm on Jul 2, 2004 (gmt 0)

10+ Year Member



It's damned hard to stop voluntary or involuntary form postings. I tried all kinds of stuff with Javascript, but never got more than say 90% reliability (due to browser incompatabilities, Javascript switched off etc).

What I did in the end was put a unique identifier in each form as a hidden field and store in the database after posting. Then, if someone reposted the same form, and the form's ID was already in the database, I could simply ignore it.

henry0

8:07 pm on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks
my form triggers an ID
so I can use your suggestion
could you be a tad more precise on how you made it
ignoring the re posting

ExpLarry

8:29 pm on Jul 2, 2004 (gmt 0)

10+ Year Member



Erm, sorry I can't much more precise without posting the code, which I can't for various reasons. Basically you have two steps:

* generate form with unique id (I use an MD5 hash, incremental numbers work fine for non-critical stuff)
* on form submission, check the ID against previously submitted IDs (usually in a database table, a file-based solution might work if you're careful about locking, maybe PHP's own session support might be useful here); if present, ignore the form submission and proceed as usual (or warn user the form has been submitted twice, or whatever); if not, store the ID and process the form.

The principle is fairly simple, the implentation depends on your needs/circumstances, available software etc.

[edit]wrote hardware, meant software[/edit]

[edited by: ExpLarry at 8:36 pm (utc) on July 2, 2004]

henry0

8:32 pm on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I might do something like

$row = mysql_fetch_row($result);
if ($row) {
echo " Form has been already submited. <a href=\"admin.php\">Please enter your admin adn etc..</a>.";
} else {
if($whatever->insertData($table,$array)) {
echo " asasasas <a href=\"****xx.php\">link</a>.";
} else {
echo "error sdfsdfsdf.";
}
}
}

<edit>
your post arrived while I was posting
ptretty much the same concept
thanks
</edit>