Usage and difference between session_unset and session_destroy in php-PHP source code

Source: Internet
Author: User
This article introduces the differences between session_unset and session_destroy functions and some different methods for deleting variables. This article introduces the differences between session_unset and session_destroy functions and some different methods for deleting variables.

Script ec (2); script

Session_unset ()

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.

The Code is as follows:
Session_start ();
Session_unset ();
Session_destroy ();
Session_write_close ();
Setcookie (session_name (), '', 0 ,'/');
Session_regenerate_id (true );
?>

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:

The Code is as follows:

$ _ SESSION ['user'] = 'user1 ′;
Session_unset ();
Session_destroy ();
?>

Method for deleting a session:

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 () is no longer used in PHP5 and can be used in the Cold palace.

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.

2. $ _ SESSION = array () delete multiple sessions

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;
Session_unset () does not cancel the session variable, but clears the values of all session variables.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.