Home  >  Article  >  Backend Development  >  How to clear all sessions in php

How to clear all sessions in php

王林
王林Original
2020-08-14 13:15:083918browse

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.

How to clear all sessions in php

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(),&#39;&#39;,time()-3600, &#39;/&#39;);
}
//彻底销毁 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!

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