Page 1 of 1

Preventing browser refresh from reposting form values -- cac

Posted: Fri Apr 21, 2006 4:15 pm
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.

Posted: Fri Apr 21, 2006 4:21 pm
by John Cartwright
use sessions to set a flag, and each time the form is submitted check the flag's existence.

Posted: Fri Apr 21, 2006 4:33 pm
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.

Posted: Fri Apr 21, 2006 4:40 pm
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

Posted: Fri Apr 21, 2006 4:56 pm
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!