Si8 Si8 - 3 months ago 17
PHP Question

Which methods destroy/unset session(s) variable(s) and work efficiently in most PHP version as well?

I am trying to unset and destroy a specific session and I came across different methods and would like to know which is the efficient method to do the work and deals with most php versions as well.

First method:


Second method:


Update: [RESOLVED]




Just use unset to unset any array index. And Session itself is an array. So use unset to unset your particular session index. You are wrong in the case of session_unset and session_destroy functions as they don't accept any parameters.

Also the point to note is that, you should not be using session_destroy as it will unset all available sessions. For eg. while loggin out, you may not be wanting to lose your products in cart formed using session.


session_destroy — Destroys all data registered to a session

session_unset — Frees all session variables

session_unset just clears the $_SESSION variable. It’s equivalent to doing:

$_SESSION = array(); So this does only affect the local $_SESSION variable instance but not the session data in the session storage.

In contrast to that, session_destroy destroys the session data that is stored in the session storage (e.g. the session file in the file system).

Everything else remains unchanged.