avoid resending forms on php pages

avoid resending forms on php pages  using -'php'

Is there a way to avoid reprocessing forms when I refresh php pages? I'd like to prevent resending forms when refreshing links to php files with an insert function in them. For example, I am processing a series of notes written by users at the top of each page for a new note. Besides the obvious creating a separate php file with a header function is there another way to do it?

asked Sep 14, 2015 by JackieMelros
0 votes

5 Answers

0 votes

Use the Post-Redirect-Get Pattern.

  1. Accept a Post request
  2. Process the data
  3. Issue a redirect response
  4. Accept a Get request
  5. Issue a 200 response

If you need to display data from the submitted stuff, then include a row id or similar in (for example) the query string of the URL you redirect to.

answered Sep 14, 2015 by KristofeGrah
0 votes

Just set some flag when you process the form first time so you could check for it and abort reprocessing later on. Session variable or cookie will work fine.

answered Sep 14, 2015 by LeonelBelmor
0 votes

The best way would be to do a header("location: form.php"); call after you process the form. That would redirect you back to the form page, and if you refresh, the browser wont resend the form data.

Alternatively, you could check to see if you already processed the data received, but that would still give you the browser warning message that you are going to resend the data.

You might do both, just in case someone uses the back button and accidentally clicks Submit again.

answered Sep 14, 2015 by ThaliaWimmer
0 votes

You could put a nonce into the page that is only allowed to be used once so that if you see the same nonce come in you don't do the insert of the page.

answered Sep 14, 2015 by Deloras29Dgs
0 votes

I redirect users to a new page after processing of the form.

The form is a POST-request to do-something.php. I check the input data and if it validates I process the data and perform a redirect to do-something.php?somethingdone. So the user can hit F5 w/o resending the POST request.

answered Sep 14, 2015 by CarGPFD