Home >Backend Development >PHP Problem >How to clear all sessions in php
php method to clear all sessions: first log out all Session variables; then use the session_destroy() function to end the current session, clear all resources, and completely destroy the Session.
Completely destroy the session
(Recommended tutorial: php graphic tutorial)
If the entire The Session session has ended. You should first log out all Session variables, then use the session_destroy() function to clear and end the current session, clear all resources in the session, and completely destroy the Session. The code is as follows:
<?php session_destroy(); ?>
session_start( ) function is used to create a Session file, and the session_destroy() function is used to close the operation of the Session and delete the Session file. TRUE is returned on success, and FALSE is returned on failure. However, this function will not release the variables related to the current Session, nor will it delete the Session ID saved in the client cookie.
The default Session in php is based on Cookie. The Session ID is stored in the Cookie of the user's browser by the server. Therefore, when logging out of the Session, you also need to clear the Session ID saved in the Cookie, and this requires the help of setcookie. () function to complete.
(Video tutorial recommendation: php video tutorial)
In the Cookie of the user's browser, the Cookie identification name that saves the Session ID is the name of the Session. This name is In php.ini, the value specified through the session.name attribute. In a php script, the name of the Session can be obtained through the session_name() function. Delete the Session ID saved in the client cookie.
The complete code is as follows:
<?php //开启 Session session_start(); // 删除所有 Session 变量 $_SESSION = array(); //判断 cookie 中是否保存 Session ID if(isset($_COOKIE[session_name()])) { setcookie(session_name(),'',time()-3600, '/'); } //彻底销毁 Session session_destroy(); ?>
The above is the detailed content of How to clear all sessions in php. For more information, please follow other related articles on the PHP Chinese website!