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

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

卡哇伊
Release: 2023-03-02 08:08:02
Original
3416 people have browsed it

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

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!

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