Updating when user go back in history

Yesterday I was working on a page where a user is inserting some things in the MySQL table.
When everything is done, they are sent to another page. But I saw that it updates everything again if I click the back button on my browser.
So I put a little code there saying that $updated=1 and then I put that on top of the first page checking if $updated=1, then the user should be transferred away with a header Location code.
But even that didn’t work. The stuff were updated anyway.

Never thought about that before… but is it common or is there a solution to solve it?

You could store some kind of token in the user’s form (like a hidden value). Then when the user makes an update, store the token in a session or cookie. If the user makes another update and the token is the same, don’t update until the token changes.

It looks like there is a single page (php script) at one URL that does 2 things:

  1. Receive POST data from the browser and update the db.
  2. Display another page (for example, record listing).

Sure, displaying the page (pt. 2) doesn’t require saving data (pt. 1) but your browser has no way of knowing that so when you go back the browser sends the POST data again thinking (reasonably) that sending the POST data is necessary to display the page. The solution is to split the two into separate pages/scripts so that the script at URL 1 receives data, saves it and then redirects the browser to URL 2, which displays the page. For this to work, page 2 obviosly needs a separate URL on its own. The most appropriate type of redirect after POST is the 303 See Other status code:


header("HTTP/1.0 303");
header("Location: http://example.com/show_something");

Thanx a lot Lemon Juice. I will try that redirect, since I think I actually did the thing on two pages after all, but still had the same problem. Not sure, since I’ve done other things after that and sitting by another computer now. But I will look a the appropriate redirect you wrote.