(PHP 4, PHP 5, PHP 7)

session_destroyLöscht alle in einer Session registrierten Daten


session_destroy ( void ) : bool

session_destroy() löscht alle in Verbindung mit der aktuellen Session stehenden Daten. Mit der Session zusammenhängende globale Variablen und das Session-Cookie werden nicht gelöscht. Um wieder Session-Variablen verwenden zu können, muss session_start() aufgerufen werden.

Hinweis: Normalerweise ist es nicht erforderlich session_destroy() aufzurufen; statt dessen sollte das $_SESSION Array bereinigt werden.

Um die Session komplett zu löschen, muss auch die Session-ID gelöscht werden. Wenn zum Verfolgen der Session ein Cookie benutzt wird (standardmäßige Einstellung), muss das Session-Cookie gelöscht werden. Dafür kann setcookie() verwendet werden.

Ist session.use_strict_mode aktiviert, dann ist es nicht nötig veraltete Session-ID-Cookies zu löschen, da das Session-Modul dann kein Session-ID-Cookie akzeptiert, wenn keine zu dieser Session-ID gehörigen Daten vorhanden sind, so dass ein neues Session-ID-Cookie gesetzt würde. Das Aktivieren von session.use_strict_mode wird grundsätzlich empfohlen.


Immediate session deletion may cause unwanted results. When there is concurrent requests, other connections may see sudden session data loss. e.g. Requests from JavaScript and/or requests from URL links.

Although current session module does not accept empty session ID cookie, but immediate session deletion may result in empty session ID cookie due to client(browser) side race condidtion. This will result that the client creates many session ID needlessly.

To avoid these, you must set deletion time-stamp to $_SESSION and reject access while later. Or make sure your application does not have concurrent requests. This applies to session_regenerate_id() also.


Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.


Beispiel #1 Löschen einer Session mit $_SESSION

// Initialisierung der Session.
// Wenn Sie session_name("irgendwas") verwenden, vergessen Sie es
// jetzt nicht!

// Löschen aller Session-Variablen.
$_SESSION = array();

// Falls die Session gelöscht werden soll, löschen Sie auch das
// Session-Cookie.
// Achtung: Damit wird die Session gelöscht, nicht nur die Session-Daten!
if (ini_get("session.use_cookies")) {
$params session_get_cookie_params();
setcookie(session_name(), ''time() - 42000$params["path"],
$params["domain"], $params["secure"], $params["httponly"]

// Zum Schluß, löschen der Session.



Verwenden Sie session_unset() nur bei veraltetem Code, bei dem nicht $_SESSION benutzt wird.

Siehe auch

add a note add a note

User Contributed Notes 6 notes

Praveen V
6 years ago
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.


[Edited by moderator (googleguy at php dot net)]
Jack Luo
4 years ago
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).

= 'nill2if998vhplq9f3pj08vjb1';
// 1. commit session if it's started.
if (session_id()) {

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

// 3. hijack then destroy session specified.

// 4. restore current session id. If don't restore it, your current session will refer to the session you just destroyed!

1 year ago
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(); }
12 years ago
Note that when you are using a custom session handler, session_destroy will cause a fatal error if you have set the session destroy function used by session_set_save_handler to private.

Fatal error: Call to private method Session::sessDestroy()

where sessDestroy was the function I specified in the 5th parameter of session_set_save_handler.

Even though it isn't all that desirable, the simple solution is to set sessDestroy to public.
2 years ago
For session_destroy() only destroy current session mean that if you specify name or change the save path of session etc  ,it will not destroy it mean for example


('testing') ;
session_start() ;

$_SESSION['id'] = '35' ;

() ;

session_destroy() ;

session_destroy only delete the new session which is created by session_start(). correct way is
('testing') ;
session_start() ;

session_destroy() ;

this is also valid for if you change path throught ini_set() , you have to mention in  delete.php.
remember session_destroy() function destroy  only current session not all .i hope this is worth to mention.
administrator at anorhack dot com
11 years ago
Destroying  a session from a background job

I have a thief-protection system that compares country codes from login IPs via whois. This has to run in the background as it is way too processor-hungry to be run in the browser.

What I needed was a way to destroy the web session from the background job. For some reason, a background session_destroy APPEARS to work, but doesnt't actually destroy the web session.

There is a work around, I set the username to NULL and the web code picks up on that, bouncing the user (thief) to a "gotcha" page where his IP is logged.

Yes I know its nasty and dirty, but surprisingly it works.

$sid = the session_id() of the suspicious web session, passed in $argv to the background job

The trick is to "stuff" the $_GET array with the sid, then the session_start in the background job picks this value up (as if it were a genuine trans-sid type thing...?PHPSESSID=blah) and "connects to" the web session. All $_SESSION variable can be viewed (and CHANGED , which is how this kludge works) but for some reason (that no doubt someone will illuminate) they can't be unset...setting the particular variable to NULL works well though:

// prove we are getting the web session data
foreach($_SESSION as $k => $v) echo($k."=".$v);
// now kill the thief
//web session variable now NULL - honestly!
To Top