PHP SESSION Destruction Session Manual Reference

session_destroy

(PHP 4, PHP 5, PHP 7)

session_destroyDestroy all data in a session

illustrate

bool session_destroy ( void )

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

Note : Usually, you don't need to call session_destroy() function in your code, you can directly clear the data in the $_SESSION array to achieve session data cleaning.    

In order to completely destroy the session, the session ID must also be reset. If the session ID is transmitted by cookie, then the setcookie() function needs to be called at the same time to delete the client's session cookie.

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

Warning

Deleting session data prematurely can lead to unpredictable results. For example, when there are concurrent requests from JavaScript or URL links, one request deletes the data in the session, which will make other concurrent requests 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, which in turn causes the client to generate a lot of inconsistencies. Necessary session ID cookie.

To avoid this from happening, you need to set a timestamp in $_SESSION after which access to the session will be denied. Alternatively, make sure there are no concurrent requests in your app. The same rule applies to session_regenerate_id() . session_regenerate_id() also.

return value

Return on success TRUE, or return on failure FALSE.

example

Example #1 Destroying session data and $_SESSION

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

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

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

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

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_paramsGet session cookie parameters, return an array

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324592613&siteId=291194637