What methods destroy / delete session variables (s) and work effectively in most PHP versions?

I am trying to disconnect and destroy a specific session, and I came across different methods and would like to know which one is an effective method to work and deals with most php versions.

First method:

unset($_SESSION['site1']); session_destroy(); 

Second method:

 session_unset(); session_destroy(); 

Update: [RESOLVED]

 <?php session_start(); unset($_SESSION['site1']); ?> 
+4
source share
3 answers

Just use unset to undo any array index. And the session itself is an array. So use unset to disable your specific session index. You are mistaken in the case of the session_unset and session_destroy functions, because they do not accept any parameters.

It should also be noted that you should not use session_destroy , as it will disconnect all available sessions. E.g. when you exit the system, you may not want to lose your products in the basket formed by the session.

Edit

session_destroy — Destroys all data registered to a session

session_unset — Frees all session variables

session_unset just clears the $ _SESSION variable. Its equivalent:

$_SESSION = array(); Thus, this only affects the local instance of the $ _SESSION variable, and not the session data in the session store.

In contrast, session_destroy destroys session data that is stored in the session store (for example, a session file in the file system).

Everything else remains unchanged.

+6
source

session_unset does not accept any parameters. It frees ALL session variables.

+3
source

Once you have called session_start() , you should treat $_SESSION just like any other object.

Because of this, unset will work just fine.

+1
source

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


All Articles