There are many ways to delete sessions in php. This article mainly introduces three methods for clearing sessions, if you are interested, follow the editor to learn how to delete and clear sessions. If we have defined the settings, we can clear the specified variables. Otherwise, we will accidentally clear all sessions, the following is a summary.
Method 1:Unset ($ _ SESSION ['xxx']) deletes a single session. unset ($ _ SESSION ['xxx']) is used to unregister a registered session variable.
It works the same as session_unregister.
Session_unregister () has been deprecated in PHP5.
Official php session deletion method
<? Php // initialize session. session_start ();/*** delete all session variables .. unset ($ _ SESSION [xxx]) can also be deleted one by one. * ***/$ _ SESSION = array ();/*** Delete sessin id. because the session is cookie-based by default, setcookie is used to delete the cookie containing the session id. * **/if (isset ($ _ COOKIE [session_name ()]) {setcookie (session_name (), '', time ()-42000 ,'/');} // Finally, the session is completely destroyed. session_destroy ();?>
Unset ($ _ SESSION) is not available. It destroys the global variable $ _ SESSION and there is no feasible way to restore it. You can no longer register the $ _ session variable.
Method 2:Session_unset () or $ _ SESSION = array () delete multiple sessions
Method 3:Session_destroy () ends the current session and clears all resources in the session. This function does not unset (release) The global variables related to the current session, nor delete the client session cookie. the default session of PHP is cookie-based. to delete a cookie, you must use the setcookie () function.
Summary:
Session_destroy is used to cancel all session variables and end session;
If you want to delete some session data, you can use the unset () function or session_destroy () function. The unset () function is used to release the specified session variable. The call format is as follows:
<?phpunset($_SESSION['jugelizi']);?>
Session_destroy () is used to delete all sessions. The call format is as follows:
<?PHP session_destroy(); ?>
Tip: session_destroy () will reset the session, and you will lose all the saved session data.
Session_unset () does not cancel the session variable, but clears the values of all session variables.
The above content is all described in this article. I hope you can help me.