Home >Backend Development >PHP Problem >How to log out the session when the PHP browser closes the page

How to log out the session when the PHP browser closes the page

藏色散人
藏色散人Original
2020-07-13 09:15:434111browse

How to close the session in PHP browser: first use "session_start();" to open the session; then clear the session information; then clear the client sessionid; finally, use "session_destroy();" to completely destroy the session.

How to log out the session when the PHP browser closes the page

#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();

For more related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to log out the session when the PHP browser closes the page. 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