What is the difference between session_unset() and session_destroy() in PHP?
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.
Difference between unset($_SESSION[]), session_unset() and session_destroy() in PHP
Its pretty simple,
session_unset — Free all session variables, but session id will not be destroy
session_destroy — Destroys all data registered to a session, to call this function first session should be registered.
unset($_SESSION['VARIABLE_NAME']) - This will unset variable value which you passed.
In your example, calling session_destroy() directly is not correct, as a result you can see the values of variable which are there in session, you can call session_destroy for registered session.
Thanks
Amit
Do you need to use session_unset before session_destroy?
You can find the following information on the official documentation (https://php.net) about session_destroy
:
It does not unset any of the global variables associated with the session, or unset the session cookie.
source: http://php.net/manual/en/function.session-destroy.php
And the documentation of session_unset
says the following:
The
session_unset()
function frees all session variables currently registered.
source: http://php.net/manual/en/function.session-unset.php
So with these informations you have to call the following to clear a session completely:
session_unset();
session_destroy();
You don't want to clear the whole session?
In case your are using a system to login and logout a user, you can also remove specific fields of the session using unset
:
unset($_SESSION['username']);
unset($_SESSION['other_user_data']);
In this case you only remove data of the user and not data for other parts of your application not related to the user.
Why do we need both, unset and session destroy when we are doing LOGOUT.php?
session_unset() delete only a variables from session - session still exist-Only data are truncated.But session_unset() is an outdated PHP function. We can set the session to an empty array instead.
$_SESSION = array();
session_destroy() will delete whole session.
It's not always necessary to do both.But it is advisable to do both just to ensure extra security.
Session unset, or session_destroy?
Unset will destroy a particular session variable whereas session_destroy()
will destroy all the session data for that user.
It really depends on your application as to which one you should use. Just keep the above in mind.
unset($_SESSION['name']); // will delete just the name data
session_destroy(); // will delete ALL data associated with that user.
PHP Unset Session Variable
You can unset session variable using:
session_unset
- Frees all session variables (It is equal to using:$_SESSION = array();
for older deprecated code)unset($_SESSION['Products']);
- Unset only Products index in session variable. (Remember: You have to use like a function, not as you used)session_destroy
— Destroys all data registered to a session
To know the difference between using session_unset
and session_destroy
, read this SO answer. That helps.
is session_unset() the correct way to clear and reset PHP Sessions variables?
session_destroy() function:
It destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie.
Note: If it’s desired to kill the session, also delete the session cookie. This will destroy the session, and not just the session data.
session_unset() function:
It deletes only the variables from session and session still exists. Only data is truncated.
You can visit this link for more information and find out what you really need.
You can also see the examples describe here
Related Topics
How to Change Maximum Number of Post Variable in PHP
PHP Pdo Prepared Statement - MySQL Like Query
Running Command-Line Application from PHP as Specific User
Fatal Error: Cannot Use Object of Type MySQLi_Result
MySQLi_Fetch_Array While Loop Columns
How to Setup Elasticsearch Index Structure With Multiple Entity Bindings
How to Detect a Create, Update, Delete Query Is Successful in Codeigniter
Laravel Migration: Unique Key Is Too Long, Even If Specified
How to Import a .SQL File in MySQL Database Using PHP
Best Practice Multi Language Website
How to Write to the Console in PHP
Get Absolute Path of Initially Run Script
What Is the Function _Construct Used For
Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error