Summary of how to delete and clear sessions in php, phpsession_PHP tutorial

Source: Internet
Author: User
Tags php session
Summary of the methods for deleting and clearing sessions in php, phpsession. Summary of methods for deleting and clearing sessions in php, phpsessionsession deletion and clearing is very important. if we define it, we can clear the specified variables. Otherwise, we will accidentally clear all sessions and summarize the methods of deleting and clearing sessions in php. phpsession

Session deletion clearing is very important. if we define it, we can clear the specified variables. Otherwise, we will accidentally clear all sessions. let's take a look at some of the summary below.

Method 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 () has been deprecated in PHP5.

Official php session deletion method

<? Php // initialize session. session_start ();/*** delete all session variables .. unset ($ _ SESSION [xxx]) can also be deleted one by one. * ***/$ _ SESSION = array ();/*** delete sessin id. because the session is cookie-based by default, setcookie is used to delete the cookie containing the session id. * **/if (isset ($ _ COOKIE [session_name ()]) {setcookie (session_name (), '', time ()-42000 ,'/');} // Finally, the session is completely destroyed. session_destroy ();?>

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.

Method 2:Session_unset () or $ _ SESSION = array () delete multiple sessions

Method 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;

If you want to delete some session data, you can use the unset () function or session_destroy () function. The unset () function is used to release the specified session variable. The Call format is as follows:

<?phpunset($_SESSION['jugelizi']);?>

Session_destroy () is used to delete all sessions. The Call format is as follows:

<?PHP session_destroy(); ?>

Tip: session_destroy () will reset the session, and you will lose all the saved session data.

Session_unset () does not cancel the session variable, but clears the values of all session variables.

The above content is all described in this article. I hope you can help me.

Deleting and clearing reset sessions is very important. if we define this function, we can clear the specified variables. Otherwise, we will accidentally clear all sessions...

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.