Lesson 4 of 9 · 22m
Session Security: IDs, Cookies, Rotation, and Expiry
Session management is a lifecycle — issue a strong ID, protect it in transit, rotate it on privilege changes, expire it deliberately, and destroy it completely.
A PHP session looks simple: call session_start(), put values in $_SESSION, logout with session_destroy(). In production, the session is your authentication perimeter. If an attacker can predict it, steal it, fix it before login, or keep using it forever, the rest of your password policy is decoration.
Configure the cookie before the session starts
The session ID usually travels in a cookie, which means cookie attributes are security controls:
session_set_cookie_params(['path' => '/', 'secure' => true, 'httponly' => true, 'samesite' => 'Lax']);
session_start();Secure keeps the cookie off plaintext HTTP. HttpOnly keeps it away from JavaScript. SameSite=Lax reduces cross-site sending for many requests. These are complements, not replacements for CSRF tokens or XSS defenses.
Rotate on privilege change
Session fixation is simple: attacker gives the victim a known session ID, victim logs in, attacker uses the same ID. PHP gives you the primitive, but you must call it at the right moments:
if (password_verify($password, $user['password_hash'])) {
session_regenerate_id(true);
$_SESSION['user_id'] = (int) $user['id'];
}Rotate after login, privilege elevation, password change, and any transition from anonymous to authenticated state. The true argument deletes the old session file or record instead of leaving a valid parallel key behind.
Expire idle and absolute time separately
Idle timeout asks "has this browser been quiet too long?" Absolute timeout asks "how long since this session authenticated?" They answer different risks.
$now = time();
$idleExpired = isset($_SESSION['last_seen']) && $now - $_SESSION['last_seen'] > 1800;
$absoluteExpired = isset($_SESSION['authenticated_at']) && $now - $_SESSION['authenticated_at'] > 43200;
if ($idleExpired || $absoluteExpired) logout_and_redirect();
$_SESSION['last_seen'] = $now;High-risk actions deserve a fresh authentication check rather than blind trust in any non-expired session.
Store less in the session
The session is a lookup key plus a small amount of trusted state, not a shopping cart database copied into every request. Store the user ID, authentication timestamp, CSRF token references, and perhaps a compact permission version. Re-read mutable authorization state from its authoritative store when correctness matters.
Logout means invalidation
Clear $_SESSION, expire the cookie, call session_destroy(), and invalidate server-side records. A copied session ID does not care that the victim pressed logout unless the server forgets it.
Operational details that bite
File-based sessions on shared hosting can expose IDs to neighbors. Custom session handlers must lock and expire correctly. Load-balanced deployments need shared storage or sticky behavior chosen deliberately. And never put session IDs in URLs; they end up in logs, browser history, referrers, and screenshots.
Field notes
- Enable
session.use_strict_modeso PHP refuses uninitialized attacker-chosen IDs instead of helpfully creating them. - Regenerating the ID on every request sounds safer but creates race conditions for parallel requests and offers little benefit over rotation at meaningful transitions.
- Never store plaintext passwords, reset tokens, or long-lived API keys in session data.
- Test login, logout, back-button behavior, and two tabs together; session lifecycle bugs love concurrency.
Was this lesson helpful?