Skip to content

Instantly share code, notes, and snippets.

@andybeak
Last active October 21, 2019 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andybeak/979822acc10a4dc973da726ca3981813 to your computer and use it in GitHub Desktop.
Save andybeak/979822acc10a4dc973da726ca3981813 to your computer and use it in GitHub Desktop.
Log out a user using server-side sessions
<?php
// can't log out if the session isn't started
session_start();
// emptying the session data is useful if your script continues running after logging out
$_SESSION = [];
// set the cookie to expire immediately
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// destroy the session on the server
session_destroy();
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment