Creating a Logout Page

Logout should remove server-side session data, expire the session cookie and return the user to a safe public page.

Logout script

Create logout.php:

<?php
session_start();

$_SESSION = [];

if (ini_get("session.use_cookies")) {
    $parameters = session_get_cookie_params();

    setcookie(
        session_name(),
        "",
        time() - 42000,
        $parameters["path"],
        $parameters["domain"],
        $parameters["secure"],
        $parameters["httponly"]
    );
}

session_destroy();

header("Location: login.php");
exit;

Clearing $_SESSION removes values in the current request. Expiring the cookie removes the browser’s session identifier. session_destroy() removes the stored session.

Logout control

For a basic local classroom project:

<a href="logout.php">Log out</a>

For a stronger design, use a POST form and a CSRF token so another website cannot trigger logout unexpectedly.

Test

  1. Log in and open a protected page.
  2. Log out.
  3. use the Back button and refresh.
  4. enter the protected URL directly.
  5. confirm the application requires login again.

Check


Revision #2
Created 2026-06-08 08:53:47 UTC by Mr Napper
Updated 2026-08-18 23:49:54 UTC by Mr Napper