Linas
Linas

Reputation: 4408

Prevent user from submiting same post data

So as the title says i want to prevent user from keep submiting $_POST data to my database.

Now what i have is form and simple class for submiting form data to my database. The problem is that if user submits data and refreshes browser it would submit same data again.

I know i could refresh page by my self with meta or by using headers, but i don't wanna do something so stupid, so i guess i could just do something like $_POST = null; not sure if that works but i actualy wanna keep all post data because if some errors appears i want to populate my form with previous post data...

Anyways i hope you guys get what i wanna do here and can help me a little :D

Upvotes: 1

Views: 702

Answers (2)

Nonym
Nonym

Reputation: 6299

I just want to post this piece of code which can help when a certain type of situation is encountered:

A form is submitted and gets processed, but somewhere after the
  processing code for the form is done some error occurs or
  the internet connection of the client is lost and sees just a white page,
  user is likely to refresh the page and will get that message box
  that asks them if they want to re-post the data they sent.
  For some users, they will try to re-post/re-send the form data
  they filled up.. 

Here's sample code:

# code near the very top of the page that processes the form
# check if $_POST had been set to session variable already
if (!isset($_SESSION['post_from_this_page'])){
    $_SESSION['post_from_this_page'] = $_POST;
} else {
    # if form has been submitted, let's compare
    if (isset($_POST)) {
        $comparison = array_diff($_POST, $_SESSION['post_from_this_page']);
        if (!empty($comparison)){
            # there are changes in the data. not a simple F5 or refresh 
            # posted data is not the same as previously posted data
            // code to handle the posting goes here, or set :
            $shouldprocessflag = true
        } else {
            # no changes, session variable (last submitted form of this page)
            # is the same as what has just been posted
            $shouldprocessflag = false;
# or perhaps use the code that @Shakti placed to redirect the user! :)
        }
    }
}

# pulled processing code from comparison check to this part
if ($shouldprocessflag = true) {
    # start processing here
}

I didn't think this would look properly formatted as a comment but I'd still like to share this idea..

Upvotes: 1

Shakti Singh
Shakti Singh

Reputation: 86476

The simple solution is you should redirect users after form submission and procession on that data.

You could make a check if the data is successfully submitted and processed redirect users otherwise don't redirect them this can keep $_POST data to re-populate fields.

This will prevent re-submission of form.

General Pseudocode

if (isset($_POST['submit']))
{
    if(validate() == true)
    {
        //passed the validation
       // do further procession and insert into db
       //redirect users to another page 
      header('location:someurl'); die();
    }
    else
    {
       $error='Validation failed';
       // do not redirect keep on the same page 
       // so that you have $_POST to re populate fields
    }   

}

Upvotes: 4

Related Questions