Difference between php session_destroy () and session_unset ()

Source: Internet
Author: User
Tags foreach session id php code

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 ();
?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.