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?
6 Answers
Use the Post-Redirect-Get Pattern.
- Accept a Post request
- Process the data
- Issue a redirect response
- Accept a Get request
- 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.

- 914,110
- 126
- 1,211
- 1,335
-
What do you mean by accept a get request and issue a 200 response? – Charles Murray Jan 16 '12 at 16:33
-
The browser issues a request with "GET /url/you/redirected/to HTTP/1.1" in it, and your server responds with "HTTP/1.1 200 OK", some other headers and some content. – Quentin Jan 16 '12 at 16:36
-
The get request is issued by the browser only if the user refreshes the page? What would it look like in PHP? – Charles Murray Jan 16 '12 at 16:45
-
No, it is issued by the browser as a reaction to getting a redirect response. It looks just like any other request that PHP will get (with no data in `$_POST`). – Quentin Jan 16 '12 at 16:48
-
What specifically is a redirect response? "header"? – Charles Murray Jan 16 '12 at 16:50
-
A response with a status code in the 300 range and a `Location` header. – Quentin Jan 16 '12 at 16:50
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.

- 6,668
- 1
- 24
- 38
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.

- 17,988
- 6
- 44
- 60
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.

- 462
- 2
- 6
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.

- 3,679
- 1
- 23
- 27
I tried to use header("Location:..."), $_POST = array(), unset($_POST)
, but (idk why) they didn't work in my php page.
So, what I did, I just used
echo '< script>window.location.replace("http://.../this.php")</script>'
it works very good! Maybe it is not a good idea, I am learning PHP for the 4th week.
-
You are likely setting the header too late, after you have already produced output. This here is a terrible workaround. https://stackoverflow.com/q/8028957/476 – deceze Dec 25 '18 at 13:55