Printing confirmation message after redirect

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

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.

Here is the code

$clean  = str_irreplace($_SERVER['QUERY_STRING'], '', $url);


$_session['message'] = ' Your firm has been submitted';

header("Location: $clean?confirmed");

$message = $_SESSION['message'];

if(!empty($message)){echo $message}

This shows the message once it redirect to confirmed query.
But how can i make it work only if submit have been posted.

1 Like

I have tried using

$_SERVER['message']  = '';

this was to clean up the session and make it blank so the once the user leaves the confirm page his session is emptied so no message is printed

  1. 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.
  2. You need to kill the script after header redirects or the rest of the code will still run.
  3. There is no Php function called str_irreplace. Is that a function you created?
  4. There is a “better” way to do Session based Flash Messages in OOP but I dont think you are there with this project.

Lol, Sorry for typos, is str_ireplace()

Just unset the message once you extract it:

$message = null;
    $message = $_SESSION['message'];
1 Like

@ahundiak thanks a million times, i so much appreciate.

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.

    $_SESSION['message'] = ' Your firm has been submitted';
    header("Location: $clean");

if (isset($_SESSION['message'])) {
    echo $_SESSION['message'];

1 Like

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 agree. I wouldn’t do flash messages like this. Just going off what the OP posted.

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 get that and do the same. Funny, looking at my code it is like what you posted. Lets go to code so we are on the same page…

class Flash
    public static function addMessage($message, $type = 'success')
        if (! isset($_SESSION['flash_notifications'])) {
            $_SESSION['flash_notifications'] = [];

        $_SESSION['flash_notifications'][] = [
            'body' => $message,
            'type' => $type

    public static function getMessages()
        if (isset($_SESSION['flash_notifications'])) {
            $messages = $_SESSION['flash_notifications'];

            return $messages;

Set Flash messages before a redirect…

Flash::addmessage('Good Job', 'success');
Flash::addmessage('Caution!', 'warning');
Flash::addmessage('Fatal Error', 'danger');

Display on redirected page…

<link rel="stylesheet" href="" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"
<?php foreach(Flash::getMessages() as $message): ?>

    <div class="alert alert-<?= $message['type'] ?>">
        <?= $message['body'] ?>

<?php endforeach ?>

1 Like

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.


Thanks @rpkamp i used both methods and it was fantastic, i noticed am perfect using it after redirection.

You guys don’t know how many hours i have spent trying this thing that you people have simplified for me, indeed knowledge is power

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.