Issue
For a long time, I've been thinking what's best way to display successful or error message. I need to refresh the page and then view the message. If I would use $_GET
(for example:/My-Page?status=success
), the problem would be that anyone can edit status
. It's not a big problem, but I don't know if it's the best idea...
If I use SESSION
:
$_SESSION['message'] = "Mail was sent successfully!";
header('Location: '.$_SERVER['REQUEST_URI']);
exit();
//and then:
if (!empty($_SESSION['message'])) {
echo $_SESSION['message'];
}
I would probably have done what I wanted, but if someone would left and then came back to the page (or just refreshed the page), he will still see a message (and that's not what I want)...
So, to sum up, my question is: What's the best way to save the message to variable to only appear once (after the refresh, the message would disappear) and nobody could edit it? I'm sorry for my English, I hope I wrote it all clearly :-)
Solution
The simplest option is to slightly amend your session based code to clear the session data after displaying it
if (!empty($_SESSION['message'])) {
echo $_SESSION['message'];
unset($_SESSION['message']); //will not display again
}
Answered By - Steve Answer Checked By - Gilberto Lyons (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.