When you have finished using a session variable, you can delete it, or you can destroy it after you have completed a conversation. If the user wants to quit the WEB system, he will need to provide him with a logout function to destroy all his information on the server.
There are 3 ways to delete a session, to delete a single session, to delete multiple sessions, and to end the current session, and there are 3 ways to make a brief introduction to each.
(1) Delete a single session
Deleting a single session deletes a variable of a single session, as with an array, and unregisters an element of the $_session array directly.
For example: $_session[' user ' variable, you can use the unset () function, as shown in the code below:
unset ($_session[' user ');
Note: When using the unset () function, be aware that elements in the $_session array cannot be omitted, that is, you cannot unregister the entire array at once, which disables the functionality of the entire session, such as the unset ($_session) function, which $_session the global variables. And there is no way to reply to it, the user can no longer register the $_session variable.
If you want to delete multiple or all sessions, you can use the following two methods.
(2) Delete multiple sessions
If you want to delete all the variables that a user registers in the session, that is, to delete multiple sessions and log off all session variables at once, you can do so by assigning an empty array to $_session, whose code is shown below:
$_session = Array ();
(3) ending the current session
If the entire session has ended, you should first unregister all session variables, then use the Session_destroy () function to clear the end of the current session and empty all the resources in the session, and the code is displayed as follows:
Session_destroy ();
Relative to the Session_Start () function (create session file), the Session_destroy () function is used to close the operation of the session (delete session file), if successful then return to TURE, destroy session data failed to return FALSE. However, the function does not release the variables associated with the current session, nor does it delete the session ID saved in the client Cookie.
PHP default session is cookie-based, session ID is stored in the client's cookie by the server, so you also need to clear the session ID stored in the cookie when you log off the session, which must be Setcookie () function to complete. In a cookie, the cookie identifying name that holds the session ID is the name of the session, which is the value specified by the Session.name property in PHP.ini. In a PHP script, you can get the name of the session through the Session_name () function. Delete the Session ID that is saved in the client Cookie.
The previous explanation can summarize the Session's deletion and logoff process, which requires several steps. The following will provide the complete code through an instance, which, after running the script, can close the session and destroy all resources related to this session. Its code is as follows:
<?php //Open Session session_start (); Delete all Session variables $_session = Array (); Determine if the Session ID is saved in the cookie ( isset ($_cookie[session_name ())) { Setcookie (Session_name (), ", Time ()-3600, '/'); } Completely destroy Session Session_destroy ();? >
Note: emptying the $_session array with $_session = Array () also clears the contents of the SESSION file that the user has on the server side. When using the Session_destroy () function, the user is removed from the session file corresponding to the server side.