Home  >  Article  >  Backend Development  >  How to delete session file in php?

How to delete session file in php?

coldplay.xixi
coldplay.xixiOriginal
2020-07-23 10:49:023208browse

How to delete session files in php: 1. Use [unset($_SESSION['xxx'])] to delete a single session; 2. Use [session_unset()] to delete multiple sessions; 3. Use [session_destroy ()】End the session.

How to delete session file in php?

php method to delete session file:

First way: unset($_SESSION['xxx'])Delete a single session, unset($_SESSION['xxx']) Used to unregister a registered session variable.

Its function is the same as session_unregister().

session_unregister() is obsolete in PHP5.

PHP official way to delete session

<?php
     // 初始化session.
     session_start();
     /*** 删除所有的session变量..也可用unset($_SESSION[xxx])逐个删除。****/
     $_SESSION = array();
     /***删除sessin id.由于session默认是基于cookie的,所以使用setcookie删除包含session id的cookie.***/
     if (isset($_COOKIE[session_name()])) {
        setcookie(session_name(), &#39;&#39;, time()-42000, &#39;/&#39;);
     }
     // 最后彻底销毁session.
     session_destroy();
?>

unset($_SESSION) This function must not be used, it will set the global variable $_SESSION destroyed, and there is no feasible way to restore it. Users can also no longer register $_session variables.

Second method: session_unset()or$_SESSION=array()Delete multiple sessions

Third method: session_destroy()End the current session and clear all resources in the session. This function will not unset (release) global variables related to the current session, nor will it delete the client's session cookie. PHP's default session is based on cookies. If you want to delete cookies, you must use the setcookie() function.

Summary:

session_destroy is to log out all session variables and end the session session;

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

<?php
unset($_SESSION[&#39;jugelizi&#39;]);
?>

session_destroy()The function of the function is to delete all sessions. The calling format is as follows:

<?PHP session_destroy(); ?>

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

session_unset()Does not unregister session variables, but clears the values ​​of all session variables.

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of How to delete session file in php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn