Session_unset () and session_destroy () usage analysis

Source: Internet
Author: User
Tags php tutorial

This section describes the functions of session_unset () and session_destroy.

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

Session_destroy ()
Delete the session file corresponding to the current user and release the session
Id. The $ _ SESSION variable in the memory is retained.

If (session_destroy ())
        {
ShowMsg ("logout successful! ", '/Member/login ');
Exit ();
        }
Else
        {
Unset ($ _ SESSION );
ShowMsg ("logout successful! ", '/Member/login ');
Exit ();
        }


Therefore, to release all resources of a user's session, execute the following code in sequence:
Program code

<? Php Tutorial
$ _ SESSION ['user'] =
'Lowell ';
Session_unset ();
Session_destroy ();
?>

Analysis by another user

Session_unregister is used to cancel a session variable;
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.

In general, you can do this when performing the exit operation:

Session_start ();
Session_unset ();
Session_destroy ();
Header ("location: XXX. php ");

There are many different ways to destroy variables in php. For example: unset (); session_unset (); session_destroy (); but as a special variable, different destruction methods will produce different results.

When unset () is used in php3, a boolean value is returned. However, in php4 and later versions, it is only a statement with no return value. If a global variable of unset () in the function or a value passed by reference, the variable can only be destroyed locally, but the variable in the call environment will keep calling unset () the same value before. If a static variable is unset, the static variable and all its references will be destroyed. If you want to unset a globals variable, you can use the GLOBALS array to destroy it: unset ($ GLOBALS ['str']);

Unset ($ _ SESSION ['str']) deletes a single $ _ SESSION ['str'] variable. If unset ($ _ SESSION) is used, all SESSION files on the server are deleted.

Session_unset () deletes all current session values. The result is the same as session_destroy ();

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.