-1

Possible Duplicate:
Preventing duplicate form submissions

I set a condition that is

if(isset($_POST)){
    //do some action
}

For the first time the condition is checked, it works fine but if the user refreshes the page it does the action 2nd time. How should I check?

Community
  • 1
  • 1

5 Answers5

0

You can unset your submit button after insert or update in database for e.g unset($_POST['btn_submit']);

Hkachhia
  • 4,463
  • 6
  • 41
  • 76
  • If the page is refreshed, the browser will resubmit the data, and `$_POST` will be repopulated. – Quentin Feb 22 '12 at 10:11
  • unset() destroys the specified variables. Check this :http://php.net/manual/en/function.unset.php – Hkachhia Feb 22 '12 at 10:16
  • 1. User submits form data. 2. PHP populates `$_POST`. 3. PHP updates database. 4. PHP deletes data from `$_POST`. 5. User **resubmits form data** by refreshing. 5. PHP **repopulates** `$_POST` with the user submitted data. 6. PHP updates database. 7. PHP deletes data from `$_POST`. – Quentin Feb 22 '12 at 10:20
  • Unsetting the variable does no good because it gets set again by the action of making the HTTP request! The script is running afresh! – Quentin Feb 22 '12 at 10:20
0

To prevent double post, by usage of browser-refresh, consider this solution.

Preventing duplicate form submissions

http://en.wikipedia.org/wiki/Post/Redirect/Get

Community
  • 1
  • 1
linuxeasy
  • 6,269
  • 7
  • 33
  • 40
0

Use the Post-Redirect-Get pattern.

  1. Browser makes POST request
  2. Server modifies database and returns a Redirect response
  3. Browser makes GET request
  4. Server shows page in a 200 response

Refreshing will just reload the the page returned in step 4 without repeating step 2.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Generally, the form processing could be split 3 steps:

  • display form
  • process data
  • redirection to the results page (e.g. thank you page)

The redirection is done to avoid the form for being submitted again on refresh. So, in your case, after the processing, you could redirect the user back to the same form again. If he refreshes the page, the form will not be submitted again, since the browser was redirected and the last action was a GET request, not a POST.

To do the actual redirecting in PHP, you could use the header function:

if(isset($_POST)) {
   ...
   // redirect back to the form
   header("Location: form.php");
}
Stelian Matei
  • 11,553
  • 2
  • 25
  • 29
  • 1
    The `Location` HTTP response header takes an absolute URI, not a relative one. Don't depend on browser error correction. (At least one browser will redirect while complaining to the user about the error). – Quentin Feb 22 '12 at 10:21
  • You're right. I checked the HTTP specs http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30 and it requires an absolute URI. I didn't know that, thanks for the tip. – Stelian Matei Feb 22 '12 at 10:26
0

Refresh problem can be solved by redirecting the page after your operation completed succesfully

if(isset($_POST))
{ 
    //  do some action 


// redirect to same page so that values will not get posted again
header("location:samepage.php"); 
}
Naveen Kumar
  • 4,543
  • 1
  • 18
  • 36
  • The `Location` HTTP response header takes an absolute URI, not a relative one. Don't depend on browser error correction. (At least one browser will redirect while complaining to the user about the error). – Quentin Feb 22 '12 at 10:21