Search code examples
phpsessionechologoutdestroy

Start session after destroy php


I have a logout file for my web application. I would like to create a session after the logout so that I can echo a logout success message. My logout code works but I am unable to create a new session after destroying the previous one.

What is the best way to do this, see code:

//LOGOUT.PHP
session_start(); 

//// unset all $_SESSION variables
session_regenerate_id();
session_unset();
session_destroy();

$_SESSION['logoutsuccess'] = 'You have successfully logged out.';
header("Location: /login/");
exit;


//LOGIN.PHP (ECHO SUCCESS MESSAGE)

if(isset($_SESSION['logoutsuccess']))
{
echo '<div class="success">'.$_SESSION['logoutsuccess'].'</div>'; 
unset($_SESSION['logoutsuccess']);
}

I would like to avoid passing variables in the url if possible.


Solution

  • Instead of trying to store it as a session variable, you could check the referer and check for /logout/

    if(strpos($_SERVER['HTTP_REFERER'], 'logout')  !== false) {
      echo 'You have successfully logged out.';
    }