Session_unset ()
You shoshould know that on recent PHP only the first one of these functions works correctly. and if you use the other two, var_dump will print you the result you expected (session cleaned up), but the session file on the server won't be cleaned up. so use the first one.
| The code is as follows: |
Copy code |
|
<? Php
Function session_clean1 ($ logout = false)
{
$ V = array ();
Foreach ($ _ SESSION as $ x => $ y)
If ($ x! = "Redirector" & ($ x! = "User" | $ logout ))
$ V [] = $ x;
Foreach ($ v as $ x)
Unset ($ _ SESSION [$ x]);
Return;
}
Function session_clean2 ($ logout = false)
{
Foreach ($ _ SESSION as $ x => $ y)
If ($ x! = "Redirector" & ($ x! = "User" | $ logout ))
Unset ($ _ SESSION [$ x]);
Return;
}
Function session_clean3 ($ logout = false)
{
$ S = ($ logout |! Isset ($ _ SESSION ["user"])? Array ():
Array ("user" = >$ _ SESSION ["user"]);
If (isset ($ _ SESSION ["redirector"])
$ S ["redirector"] = $ _ SESSION ["redirector"];
$ _ SESSION = $ s;
}
?> |
On previous php (<5.1.4) releases at least the third one worked correctly.
Releases all the $ _ SESSION variables that have been created in the memory, but does not delete the session file or release the corresponding session id.
Session_destroy ()
Delete the session file corresponding to the current user and release the session id. The $ _ SESSION variable in the memory is retained.
Therefore, to release all resources of a user's session, execute the following code in sequence:
PHP code
| The code is as follows: |
Copy code |
|
<? Php
// Initialize the session.
// If you are using session_name ("something"), don't forget it now!
Session_start ();
// Unset all of the session variables.
$ _ SESSION = array ();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
If (ini_get ("session. use_cookies ")){
$ Params = session_get_cookie_params ();
Setcookie (session_name (), '', time ()-42000,
$ Params ["path"], $ params ["domain"],
$ Params ["secure"], $ params ["httponly"]
);
}
// Finally, destroy the session.
Session_destroy ();
?> |