Start session after destroy php

10,215

Solution 1

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.';
}

Solution 2

Call session_start() again after session_destroy()?

Solution 3

Just start a new session:

session_start();
session_destroy();
session_start();
$_SESSION['food'] = 'pizza';
Share:
10,215
hairynuggets
Author by

hairynuggets

Web developer/entrepeneur. Codes php, likes codeigniter, css and jquery.

Updated on June 24, 2022

Comments

  • hairynuggets
    hairynuggets almost 2 years

    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.