Preventing browser refresh from reposting form values -- cac

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
macworks
Forum Newbie
Posts: 6
Joined: Sun Jan 23, 2005 4:25 pm
Location: Minneapoils, MN - USA

Preventing browser refresh from reposting form values -- cac

Post by macworks »

I'm wondering how to prevent a page refresh from re-posting the form values so that PHP won't re-process the same form submission twice.

For example, I'm building my own custom message board and when a poster submits their new post, they get the result page which shows that their message was successfully posted. But if they refresh the browser window, they'll end up posting the message again.

Is this where session.cache_limiter comes in?

I realize I could send another header once the post has been logged into the DB in order to forward on to yet another page (preventing this refresh scenario) but I'd prefer a slightly easier method of preventing the refresh from happening altogether.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

use sessions to set a flag, and each time the form is submitted check the flag's existence.
macworks
Forum Newbie
Posts: 6
Joined: Sun Jan 23, 2005 4:25 pm
Location: Minneapoils, MN - USA

Post by macworks »

Not sure I follow. If I set a flag when the form is presented then they submit the form and the form is processed, how is setting a flag going to do me any good if they refresh, the flag hasn't changed.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

if (isset($_SESSION['form_flag'])) {
   if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   
      //process the results

      //some sort of check to see if all the contents are valid
      //and we are ready to process the form
      if (!$form->hasErrors()) {
         $_SESSION['form_flag'] = true;
      }
   }
}
Something like that
macworks
Forum Newbie
Posts: 6
Joined: Sun Jan 23, 2005 4:25 pm
Location: Minneapoils, MN - USA

Post by macworks »

Thanks! I figured out how to make that work and have already implemented and tested it -- now I have to go back and apply this to a slew of other forms on this site.


It's sad that I couldn't see the forrest for the trees on this one!
Post Reply