Home > Backend Development > PHP Tutorial > PHP SESSION destroy session

PHP SESSION destroy session

不言
Release: 2023-03-24 10:18:01
Original
1509 people have browsed it

This article introduces the content of PHP SESSION destruction session, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

session_destroy


(PHP 4, PHP 5, PHP 7)

session_destroyDestroy all data in a session

Description

bool session_destroy ( void )

session_destroy() Destroy all data in the current session, but will not reset the global variables associated with the current session, nor will it reset the session cookie. If you need to use session variables again, you must call the session_start() function again.

Note: Normally, you do not need to call the session_destroy() function in your code, you can directly Clear the data in the $_SESSION array to implement session data cleaning.​

In order to completely destroy the session, the session ID must be reset at the same time. If the session ID is transmitted through cookies, you also need to call the setcookie() function to delete the client's session cookie.

When the session.use_strict_mode configuration item is enabled, you do not need to delete the cookie corresponding to the expired session ID, because the session module no longer accepts cookies carrying expired session IDs, and then it will generate a new one Session ID cookie. It is recommended that all sites enable the session.use_strict_mode configuration item.


Warning

Premature deletion of data in the session may lead to unpredictable results. For example, when there are concurrent requests linked from JavaScript or URL, a certain request deletes the data in the session, which will cause other concurrent requests to be unable to use the session data.

Although the current session processing module will not accept an empty session ID, due to the way the client (browser) handles it, immediately deleting the data in the session may result in an empty session cookie being generated, and thus Causes the client to generate many unnecessary session ID cookies.

In order to avoid this situation, you need to set a timestamp in $_SESSION. Access to the session after this timestamp will be denied. Or, make sure there are no concurrent requests in your application. This rule also applies to session_regenerate_id(). session_regenerate_id() also.


Return value


Returns on successTRUE, or FALSE on failure.


Example


Example #1 Destroy session data and


<?php
// 初始化会话。
// 如果要使用会话,别忘了现在就调用:
session_start();

// 重置会话中的所有变量
$_SESSION = array();

// 如果要清理的更彻底,那么同时删除会话 cookie
// 注意:这样不但销毁了会话中的数据,还同时销毁了会话本身
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), &#39;&#39;, time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

// 最后,销毁会话
session_destroy();
?>
Copy after login

<span style="color:rgb(0,0,0);"><span style="color:rgb(0,0,187);"><br></span></span>


#ini_get

(PHP 4, PHP 5, PHP 7)

ini_get — Get the value of a configuration option

Description
string ini_get (string $varname)

Returns the value of the configuration option on success.


##session_get_cookie_params

Get the session cookie parameters and return the array

Related recommendations :

PHP session to prevent repeated login##There are three ways to destroy PHP session

The above is the detailed content of PHP SESSION destroy session. 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