session_destroy
Deletes the session data on the server.
Syntax
session_destroy(): boolReturn value
bool`true` on success.
Examples
Logging a user out:
<?php
session_start();
$_SESSION = [];
session_destroy();
header('Location: /');
exit;Notes
The $_SESSION array stays filled until the end of the request — that is why it is cleared separately.
See also
- session_start — Starts a session or resumes the existing one.
- session_regenerate_id — Gives the session a new identifier, keeping the data.
- setcookie — Sends a cookie to the browser.
Related recipes
In the tutorial
- Registration and login — How to build registration and login in PHP — the users table, password hashing, checking on login, the session, protected pages and logout.