Deletion and clearing of sessions is very particular. If we define it, we can clear the specified variables. Otherwise, all sessions will be removed accidentally. Let’s take a look at some summary below.
The first method:unset($_SESSION['xxx']) Delete a single session, unset($_SESSION['xxx']) is used to unregister a registered session variable.
Its function is the same as session_unregister().
session_unregister() has been abandoned in PHP5.
php official delete session method
<?php // Initialize session. session_start(); /*** Delete all session variables.. You can also delete them one by one by unset($_SESSION[xxx]). ****/ $_SESSION = array(); /***Delete the session id. Since session is cookie-based by default, use setcookies to delete cookies containing session id.***/ if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time()-42000, '/'); } // Finally completely destroy the session. session_destroy(); ?>
unset($_SESSION) This function must not be used, it will destroy the global variable $_SESSION, and there is no feasible way to restore it. Users can no longer register the $_session variable.
The second method:session_unset() or $_SESSION=array() to delete multiple sessions
The third method:session_destroy() ends the current session and clears all resources in the session. This function will not unset (release) and the current session related global variables (global variables), nor will it delete the client's session. The default session is cookie-based. If you want to delete a cookie, you must use the setcookie() function.
summary:
session_destroy is to log out all session variables and end the session session;
If you want to delete some session data, you can use the unset() function or the session_destroy() function. The function of the unset() function is to release the specified session variable, and the call format is as follows:
<?php unset($_SESSION['jugelizi']); ?>
The function of the session_destroy() function is to delete all sessions, and the call format is as follows:
<?PHP session_destroy(); ?>
Tip: session_destroy() will reset the session, and you will lose all saved session data.
session_unset() does not cancel the session variable, but clears the values of all session variables.
The above content is the full description of this article, I hope you will be helpful.