php - Sessions is not performing the task

function display_message()
{
    if (isset($_SESSION['message'])) {
        echo $_SESSION['message'];
        unset($_SESSION['message']);
    }
}

Why when I remove the unset the code work but the problem that the message is not removed , so I need to keep can anyone tell me why the code is not work

Note

That i use session_start()

Answer

Solution:

I have faced that issue before but i fixed it in this way and tested it working in local environment

function display_message()
{

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

   unset($_SESSION['message']);
}

Instead of using unset function inside if statement, move it to outside.

Answer

Solution:

You can unset instead,destroy your session whit down code. The purpose is the name session.

session_start();

    $_SESSION['name'] = "name";
    echo "<br>" . $_SESSION['name'] . "<br>";

    $_SESSION['family'] = "family";
    $_SESSION['name'] = null;




    test the code

Answer

Answer

Answer

--------test the code if (isset($_SESSION['name'])) { echo "The code didnt work<br>"; echo $_SESSION['name']; } else { echo "The code is working<br>"; } echo $_SESSION['family'];

Source