Session Persistence After Logout: Understanding session_destroy() Behavior
The issue you are encounteringâwhere a user can still access protected pages after clicking "logout" and using the browser's back buttonâis a classic symptom of how PHP sessions interact with browser history and cache mechanisms. The root cause isn't a flaw in your logic flow; it is a fundamental misunderstanding of what session_destroy() actually does in relation to the HTTP session cookie stored on the client side.
In your logout.php, you execute unset($_SESSION['user']) and unset($_SESSION['pass']). While this clears the variables from the current request, it does not invalidate the session identifier itself. The critical line is session_destroy(). This function removes all session variables and invalidates the server-side session data for that specific ID. However, the browser typically keeps the session cookie alive if you haven't explicitly set its expiration time to zero.
When a user clicks your logout link, they are redirected to index.php. If the session cookie (usually named PHPSESSID) still exists in the browser's cookie jar, the next requestâwhether it is navigating forward or clicking the "Back" buttonâwill send that valid session ID back to the server. Since you have already called session_destroy(), the server recognizes this ID as invalid and should theoretically reject access. However, if your application relies on a flag like $_SESSION['logged_in'] to control access, and that flag wasn't explicitly set to false before destruction, or if the session is being shared across subdomains without proper isolation, issues can arise.
The specific behavior you are seeing suggests that the browser is caching the response or that the redirect is not fully terminating the session state in the way you expect. The most robust solution involves ensuring the session cookie is explicitly deleted on the client side immediately after destroying the session data on the server.
You should modify your logout.php to include a command that sets the session cookie's expiration date to a time in the past. This forces the browser to delete the cookie from its local storage, preventing any subsequent requests (including those triggered by the back button) from sending the session ID at all.
Here is how you should adjust your logout.php:
<?php
// Start session to ensure we can access and manipulate the cookie
session_start();
// Destroy server-side session data
session_destroy();
// Set the session cookie to expire in the past (e.g., -100 years)
// This forces the browser to delete the PHPSESSID cookie 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"]
);
}
// Redirect to the public index page
header("Location: ../index.php");
exit();
?>
This approach is superior to simply echoing a JavaScript redirect. Using header("Location: ...") ensures the server handles the redirection, which is crucial for maintaining security headers and preventing content leakage. The JavaScript method you used (echo "<script>...") executes in the browser context; if the user blocks JavaScript or if the page loads before the script runs, the session state might not be handled correctly by the server immediately.
Furthermore, ensure that your protected pages check for the existence of the session ID rather than just checking specific variables. Even after session_destroy(), a request might arrive with an invalid session ID. Your access control logic should look like this:
<?php
session_start();
// Check if the session exists and the user is logged in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
// Redirect to login or index if not authenticated
header("Location: ../index.php");
exit();
}
// If we are here, the user is authenticated and the session is valid
?>
By combining server-side data destruction with client-side cookie expiration, you eliminate the window where a stale session ID could be replayed via the back button. This pattern is essential for any application handling sensitive user data. If you are managing multiple projects or need a standardized authentication flow, resources like those from https://laravelcompany.com often provide insights into structured session management strategies that can further harden your application against these common pitfalls.