PHP session ($ _SESSION []) works even to destroy a session

Here is the code I'm destroying, but it still works.

<?php session_start(); $_SESSION['name'] = 'Arfan'; $_SESSION['second_name'] = 'Haider'; echo 'My full name is '.$_SESSION['name'].$_SESSION['second_name'].'<br/>'; unset($_SESSION['second_name']);// unset the second_name session echo 'My name is '.$_SESSION['name'].$_SESSION['second_name'].'<br/>';// work fine error popup session_destroy();// Destroy all the session echo $_SESSION['name']; // session is working here. ?> 

As you can see, at the end of the code session, why also works?

+4
source share
2 answers

After using session_destroy() session cookie is deleted and the session is no longer stored on the server. Values ​​in $_SESSION may be available, but they will not be loaded on the next page.

If you want to completely clear the session, you can use:

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

From docs :

In order to completely kill a session, as well as for user login, the Session ID must also be canceled. If a cookie is used to distribute session id (default behavior), then the session cookie must be deleted. Setcookie () can be used for this.

An example :

 session_start(); session_unset(); session_destroy(); session_write_close(); setcookie(session_name(),'',0,'/'); session_regenerate_id(true); 
+4
source

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


All Articles