I am using the POST/REDIRECT/GET pattern in my application. However, I have a problem:
In case I want to display a message at the GET stage, I can store it as as session variable at the POST stage (when it is decided what message should be displayed), for example $_SESSION['message']='mplampla';
Using this technique, the message should be unset after being displayed in order not to display it again erroneously in other pages. But if user the goes back and then forward to the GET page again, the message won't display the second time as it has been unset the first time around.
I don't know how other websites manage this... I have seen a web site with a registration form, which displays a message after successful registration under the same URL and works correctly when doing back/forward.
Don't store the message in $_SESSION
. Instead, pass it as a parameter to your GET page.
For example, in the GET stage, redirect the user to
http://localhost/widget.php?edit=1&message=saved
Then, in widget.php
do:
$message = isset($_GET['message']) ? $_GET['message'] : null;
$output = null;
switch($message) {
case 'completed': // possibly use a constant here, eg MESSAGE_COMPLETED
$output = 'Your changes has been saved.';
break;
case 'failed':
$output = 'ERROR: failed to save changes!';
break;
}
// Now present $output to the user anyway you like
If the user goes back and forward, the message will still be displayed just fine. It also avoids any problems having to do with session state.