SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    323
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by brad62 View Post
    Yesterday I was working on a page where a user is inserting some things in the MySQL table.
    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:

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

  4. #4
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •