Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between session_unset() and session_destroy() in PHP?

From the php.net documentation:

session_destroy — Destroys all data registered to a session

session_unset — Free all session variables

My three part question is:

The two functions seem very similar.
What is really the difference between the two?

Both seem to delete all variables registered to a session. Does any of them actually destroy the session itself? If not, how do you accomplish this (destroy the session itself).

Is it correct that neither of the two functions deletes the session cookie at the client?

like image 272
Johan Avatar asked Nov 29 '10 11:11

Johan


People also ask

What is PHP session_start () function?

Description ¶ session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

How do you empty a session variable in PHP?

Destroying a PHP Session A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.

How do you know if a session is started PHP?

In PHP,we utilize session_start() an inbuilt function to start the session .


2 Answers

session_destroy(); is deleting the whole session.

session_unset(); deletes only the variables from session - session still exists. Only data is truncated.

like image 24
Xamael Avatar answered Oct 11 '22 11:10

Xamael


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.

like image 145
Gumbo Avatar answered Oct 11 '22 12:10

Gumbo