Tutorials Logic, IN info@tutorialslogic.com

PHP Sessions: State, Login Rotation, Flash Data, and Security

Session State

A PHP session stores data on the server and links requests with an identifier, usually carried in a cookie. The browser holds the identifier, not the full $_SESSION array.

Start or resume the session before output, rotate the ID when privileges change, and remove authentication state deliberately during logout.

Start and Use a Session

Store Course Progress

Store Course Progress
<?php
session_start();

$_SESSION['completed_lessons'] = ($_SESSION['completed_lessons'] ?? 0) + 1;
echo (string) $_SESSION['completed_lessons'];

The value remains available to later requests that resume the same valid session.

Authentication Rotation

After credentials are verified and before authenticated state is stored, regenerate the session identifier. Production session rotation must account for concurrent requests and unstable networks.

Mark an Authenticated Session

Mark an Authenticated Session
<?php
session_start();

// Run only after the password has been verified.
session_regenerate_id();
$_SESSION['user_id'] = 42;
$_SESSION['authenticated_at'] = time();

Flash Data

Flash data is a value intended for one later request, such as a success message after redirect. Read and remove it together so stale messages do not repeat.

Consume a Flash Message

Consume a Flash Message
<?php
session_start();

$message = $_SESSION['flash'] ?? null;
unset($_SESSION['flash']);

if ($message !== null) {
    echo htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
}

Logout and Configuration

  • Remove authentication values and invalidate the session cookie during logout.
  • Enable session.use_strict_mode and session.use_only_cookies.
  • Use Secure over HTTPS, HttpOnly, and an appropriate SameSite setting.
  • Never expose a session ID in HTML, logs, or URLs.
  • Do not store passwords or large permanent records in session data.

Locking and Lifetime

The default file session handler locks a session while a request has it open. A slow request can therefore block another request from the same user. Read or update the required values, then call session_write_close() before long database, file, or network work.

Cookie expiration and server-side record cleanup are separate. Enforce idle and absolute authentication limits in application state, and configure garbage collection for the session handler. Browser cookie lifetime alone is not a security timeout.

  • Rotate the identifier after login, privilege elevation, and other trust changes.
  • Destroy server state and expire the matching session cookie during logout.
  • Bind authorization to current server-side user data, not a role copied once and trusted forever.
  • Regenerate carefully when parallel requests may still carry the previous identifier.

Session Security Check

0 of 2 checked

Q1. When should the session ID be regenerated?

Q2. Where is normal $_SESSION data stored?

Session Security Failures

  • Session remains locked

    Call session_write_close() before slow work once state changes are complete.
  • ID survives login

    Regenerate after credentials are accepted and before storing authenticated state.
  • Logout clears only $_SESSION

    Destroy server data and expire the session cookie with matching attributes.

Try this next

Build a Session Flow

0 of 2 completed

  1. Store a success message before redirect and consume it once on the destination page.
  2. List the session values, cookie action, and redirect needed for a complete logout.
Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.