Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shivamyadav's avatar

php session problem?

her is my code and session is started in my header and this is footer code

<div id="session-msg" class="fixed bottom-5 text-white right-5 text-md bg-blue-500 p-2 rounded-lg">
    <?php
    if (isset($_SESSION['msg'])) {
        echo $_SESSION['msg'];
        // Unset the session after displaying the message
        unset($_SESSION["msg"]);
    }
    ?>
</div>

i want to display the session msg when a Session['msg'] has and after that it should be unset when a page is refreshed something like that ... but my this logic doesn't works due to the echoing the session msg and unset() that session msg at a same time.. don't know exactly what to do

0 likes
1 reply
umerfayyaz's avatar
<?php
session_start();
    if (isset($_SESSION['msg'])) {
        echo $_SESSION['msg'];
        // Unset the session after displaying the message
        unset($_SESSION["msg"]);
    }
    ?>

try this

Please or to participate in this conversation.