php double form submit prevent

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I would like to avoid the double form submit or unwanted submit when first time clicking the link to the form which outputs empty data.

The code below seems to prevent double or single empty form or previous form submit but it also prevents to submit the form when expected.

Main parts of the code as below, all parts on the same php file.

$_SESSION['token'] = md5(session_id() . time());


<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" name="form_submitted">

<input type="hidden" name="token" value="<?php echo $_SESSION['token'] ?>" >

<input type="checkbox" name="catexp[]" value="1">Input1
<input type="checkbox" name="catexp[]" value="2">Input2
<input type="checkbox" name="catexp[]" value="3">Input3

<input type="Submit"  name="Submit" >



if (isset($_SESSION['token']))
    if (isset($_POST['token']))
        if ($_POST['token'] != $_SESSION['token'])
            // double submit


        }// else ($_POST['token'] == $_SESSION['token'])

    } // if (isset($_POST['token']))

} // if (isset($_SESSION['token']))


How to make the condition go through ?


Re: php double form submit prevent

On 12/24/2013 4:15 PM, wrote:
Quoted text here. Click to load it

Your problem is you are setting $_SESSION['token'] on entry to the  
script.  Since you've included the time(), it will never match the value  
in $_POST['token'] unless the user has submitted the form in the same  
second it was sent - highly unlikely (in fact, almost impossible  
considering network delays, the person on the other end, etc.).

A better way is to redirect the user to another page (i.e. a "thank you"  
page after processing the submission with header('Location: ...');  This  
will stop them from using the back button (but still won't stop them  
from going back in their history).

Remember you have to call header() before ANY output (even your DOCTYPE)  
is sent to the browser.  Normally this is accomplished by processing the  
form before anything is sent to the browser.

You probably could also just not set $_SESSION['token'] until after the  
checks are done; it's not something I've tried, however.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: php double form submit prevent wrote:

Quoted text here. Click to load it

Think about what is happening. :)  You're storing a new token as the
first action when the resource is requested -- obviously, any later
comparision with the submitted token will fail.  You may deploy another
variable to store the new token till the end of the script, and then
storing it in the session.

The basic algorithm you have used to prevent multiple form submission is
actually a CSRF protection (might be somewhat weak, though) -- if you
don't need it, you may consider implementing the PRG pattern[1], which
might be a better solution of the problem at hand.

[1] <

Christoph M. Becker

Site Timeline