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

藏色散人
Release: 2023-03-02 12:44:01
Original
4063 people have browsed it

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();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template