Print still writes the old value of the Session variable after destruction

I use some session variables. When I log out, the show_number function should write the number 0, but that is not the case.

logout.php:

<?php session_start(); session_destroy(); require_once('upper.php'); show_number(); //this function is declared on upper.php ?> 

index.php:

 <?php $_SESSION['var'] = 1; echo "<a href="logout.php">Logout</a> ?> 

upper.php:

 function show_number() { // shows value of $_SESSION['var']; if (isset($_SESSION['var'])) echo "1"; else echo "0"; } 

And the problem is that: When I click on the "Exit" link, the echo still writes number 1, and I need to reload the page to see the value 0.

Greetings

+1
source share
1 answer

The global $_SESSION object will not be cleared by session_destroy :

It does not cancel any of the global variables associated with the session or disable the session cookie.

To clear session data in code:

 session_start(); session_destroy(); $_SESSION = array(); 

Or for a more complete destruction of all session data (from the same documentation page):

 <?php // Initialize the session. // If you are using session_name("something"), don't forget it now! session_start(); // Unset all of the session variables. $_SESSION = array(); // If it desired to kill the session, also delete the session cookie. // Note: This will destroy the session, and not just the session data! if (ini_get("session.use_cookies")) { $params = session_get_cookie_params(); setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"] ); } // Finally, destroy the session. session_destroy(); ?> 
+5
source

Source: https://habr.com/ru/post/1482814/


All Articles