session_destroy

(PHP 4, PHP 5)

session_destroyDestroys all data registered to a session

Описание

bool session_destroy ( void )

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.

Возвращаемые значения

Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.

Примеры

Пример #1 Destroying a session with $_SESSION

<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
    
$params session_get_cookie_params();
    
setcookie(session_name(), ''time() - 42000,
        
$params["path"], $params["domain"],
        
$params["secure"], $params["httponly"]
    );
}

// Finally, destroy the session.
session_destroy();
?>

Примечания

Замечание:

Only use session_unset() for older deprecated code that does not use $_SESSION.

Смотрите также

Коментарии

Автор:
If you want to change the session id on each log in, make sure to use session_regenerate_id(true) during the log in process.

<?php
session_start
();
session_regenerate_id(true);
?>

[Edited by moderator (googleguy at php dot net)]
2012-09-14 07:39:57
http://php5.kiev.ua/manual/ru/function.session-destroy.html
Автор:
It took me a while to figure out how to destroy a particular session in php. Note I'm not sure if solution provided below is perfect but it seems work for me. Please feel free to post any easier way to destroy a particular session. Because it's quite useful for functionality of force an user offline.

1. If you're using db or memcached to manage session, you can always delete that session entry directly from db or memcached.

2. Using generic php session methods to delete a particular session(by session id).

<?php
$session_id_to_destroy 
'nill2if998vhplq9f3pj08vjb1';
// 1. commit session if it's started.
if (session_id()) {
   
session_commit();
}

// 2. store current session id
session_start();
$current_session_id session_id();
session_commit();

// 3. hijack then destroy session specified.
session_id($session_id_to_destroy);
session_start();
session_destroy();
session_commit();

// 4. restore current session id. If don't restore it, your current session will refer to the session you just destroyed!
session_id($current_session_id);
session_start();
session_commit();

?>
2014-03-27 12:08:13
http://php5.kiev.ua/manual/ru/function.session-destroy.html
Автор:
I'm using PHP 7.1 and received the following warning when implementing Example #1, above:

    PHP message: PHP Warning:  session_destroy(): Trying to destroy uninitialized session in...

What I discovered is that clearing $_SESSION and removing the cookie destroys the session, hence the warning.  To avoid the warning while still keeping the value of using session_destroy(), do this after everything else:

    if (session_status() == PHP_SESSION_ACTIVE) { session_destroy(); }
2017-07-04 18:39:30
http://php5.kiev.ua/manual/ru/function.session-destroy.html
All of a sudden neither session_destroy() nor $_SESSION=[] were sufficient to log out. I found the next to work:
<?php
setcookie
(session_name(), session_id(), 1); // to expire the session
$_SESSION = [];
?>
2020-12-09 22:21:35
http://php5.kiev.ua/manual/ru/function.session-destroy.html

    Поддержать сайт на родительском проекте КГБ