Home  >  Article  >  Backend Development  >  How to log out of SESSION when php closes the page directly

How to log out of SESSION when php closes the page directly

卡哇伊
卡哇伊Original
2020-07-08 11:51:323316browse

php method to directly close the page and log out SESSION: First, each page must turn on "session_start()" before the session can be used in the page; then the next time the user visits, session_start() will check whether there is Session ID; finally destroy the session completely.

How to log out of SESSION when php closes the page directly

#1. Session_start() must be enabled on each page before session can be used in each page.

2. Session_start() initializes the session. The first visit will generate a unique session ID and save it on the client (saved based on cookies). The next time the user visits, session_start() will check whether there is a session. ID, if there is a browser, it will bring this session ID (passed by sending a header file, which can be seen with ff browser) to determine the client.

3. The session given to the cookie will save a session ID, session_id, on the client. This can be seen by printing the cookie. The key value of this session_id is session_name, session_id() == $_COOKIE[session_name( )]

4. If the client disables cookies, you must use the url to pass the session_id, which is the SESSION given to the URL.

5. You cannot use unset($_SESSION) when logging out of the SESSION. You can use $ _SESSION = array() or $_SESSION = null. The correct method to log out the session is as follows:

//正确的注销session方法:
//1开启session
session_start();
   
//2、清空session信息
$_SESSION = array();
   
//3、清楚客户端sessionid
if(isset($_COOKIE[session_name()]))
{
  setCookie(session_name(),'',time()-3600,'/');
}
//4、彻底销毁session
session_destroy();

The above is the detailed content of How to log out of SESSION when php closes the page directly. 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