session_destroy

(PHP 4, PHP 5, PHP 7, PHP 8)

session_destroyDistrugge tutti i dati registrati in una sessione

Descrizione

session_destroy(): bool

session_destroy() distrugge tutti i dati associati alla sessione corrente. Non desetta nessuna delle variabili globali associate alla sessione o desetta il cookie di sessione.

Questa funzione ritorna true in caso di successo e false in caso di fallimento nel distruggere i dati di sessione.

Example #1 Distruggere una sessione

<?php

// Inizializza la sessione.
// Se state usando session_name("qualcosa"), non dimenticatevelo adesso!
session_start();
// Desetta tutte le variabili di sessione.
session_unset();
// Infine , distrugge la sessione.
session_destroy();

?>

Example #2 Distruggere una sessione con $_SESSION

<?php

// Inizializza la sessione.
// Se state usando session_name("qualcosa"), non dimenticatevelo adesso!
session_start();
// Desetta tutte le variabili di sessione.
$_SESSION = array();
// Infine distrugge la sessione.
session_destroy();

?>

add a note

User Contributed Notes 3 notes

up
55
Praveen V
12 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.

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

[Edited by moderator (googleguy at php dot net)]
up
24
Jack Luo
10 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).

<?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();

?>
up
-1
vitas dot lowang at gmail dot com
1 month ago
I found out that in order to really have the session data cleared after calling session_destroy(), you need to refresh the page. For example by calling
header('Location: '.$_SERVER['PHP_SELF']."?page=profile");

if you don't then there are still data in $_SESSION which was there before. I don't know if this is intended behavior but it's kind of confusing IMO...
To Top