Please house, i have spent hours on this without success.
I have a form that i did redirected to a query after submission, and i passed some variables to session so that i can echo it once we get to the confirmation page
It works and prints the confirmation message.
But then my problems is that even if the user did not submit any form and just typed site.com/?confirm
The old message stored in his session will be echoed again.
I want it to only echo once the form submit is the only way through which the confirm query was submitted.
You dont need to use a session for this “style” ($_GET) of flash messages* (keyword here). Even without the session, using $_GET will always allow this behavior.
You need to kill the script after header redirects or the rest of the code will still run.
There is no Php function called str_irreplace. Is that a function you created?
There is a “better” way to do Session based Flash Messages in OOP but I dont think you are there with this project.
If you do that block of code there is no point to ?confirmed on the redirect or the whole $message code. You would just echo the message and unset right after that.
if (REQUEST_METHOD === 'POST') {
$_SESSION['message'] = ' Your firm has been submitted';
header("Location: $clean");
die;
}
if (isset($_SESSION['message'])) {
echo $_SESSION['message'];
unset($_SESSION['message']);
}
You certainly could but I’m a big believer in extracting all your data in one section and then doing all the echo’ing in another. The render code should not care where $message comes from. Separation of concerns and all that.
I use them all the time. The Symfony session interface abstracts some of the functionality but the basic idea of storing the message in a session variable and then removing it after displaying is the same.
I don’t have any relevant code to show since I use the Symfony Session object. The object has what is called a bag of flashes. Stored flashes end up in the session. Retrieving a flash automatically removes it.
$session->getFlashBag()->set('message','Some Message');
$message = $session->getFlashBag()->get('message'); // Message is removed
Been a long time since I have used anything besides the Symfony HTTP Foundation classes for request/response/session stuff.
This is just getting interesting, am learning deeply here, please @ahundiak and @benanamen don’t stop am upgrading my skills with this, nice works and thanks alot
Just to add some terminology, the reading of flash messages after which they are gone is called a destructive read. It’s awesome for flash messages, but generally you wouldn’t use it much.