← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

session_regenerate_id()

PHP PHP 5.0+ Intermediate
debt(d5/e1/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list semgrep and phpstan as the tools that catch missing or incorrectly called session_regenerate_id() — neither is a default linter bundled with PHP, so this lands at d5. The specific pattern (no regeneration after login, or regeneration without true) requires a configured SAST rule and won't be caught by a bare compiler or syntax check.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix is explicit: replace session_regenerate_id() with session_regenerate_id(true) — a single-character/token change at the call site. Even adding the call where it's missing is a one-line insertion immediately after login. No refactor or cross-file change required.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The concept applies only to web contexts (applies_to: web) and specifically to login/privilege-escalation code paths. It doesn't shape the broader architecture — it's a localised concern confined to authentication logic. Future maintainers only need to think about it when touching session or auth code.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap — contradicts how a similar concept works elsewhere' (t7). The misconception field captures the exact trap: session_regenerate_id() without the boolean parameter looks complete and correct — the function name implies the old session is replaced — but the old session file silently persists, leaving the fixation vulnerability fully open. A competent developer naturally assumes calling the function is sufficient, making this a serious behavioural contradiction between what the name implies and what actually happens.

About DEBT scoring →

Also Known As

session_regenerate_id() session fixation prevention session rotation

TL;DR

Generates a new session ID after login — the primary defence against session fixation attacks.

Explanation

session_regenerate_id(true) creates a new session ID and optionally deletes the old session file (the true argument). It must be called immediately after successful authentication. Without it, an attacker who knows a victim's pre-login session ID (e.g. from a shared computer or network sniff) can use the same ID after the victim logs in and gain authenticated access. The true parameter is important — without it the old session file persists and is still usable.

Common Misconception

session_regenerate_id() without parameters is sufficient. Without passing true, the old session file is not deleted — an attacker who obtained the old ID can still use it until it expires. Always call session_regenerate_id(true) after login.

Why It Matters

Regenerating the session ID after authentication invalidates the pre-login session ID — an attacker who forced a known session ID on the victim can no longer use it after the victim logs in.

Common Mistakes

  • Not passing true to session_regenerate_id() — the old session file persists without the delete flag.
  • Calling session_regenerate_id() before session_start() — has no effect.
  • Not regenerating after privilege level changes — elevating to admin without regenerating is still a fixation risk.
  • Regenerating on every request — unnecessary overhead and can break concurrent AJAX requests.

Code Examples

✗ Vulnerable
// Login without session regeneration — fixation vulnerability:
session_start();
if (authenticate($user, $pass)) {
    $_SESSION['user_id'] = $user->id;
    // Missing: session_regenerate_id(true);
    header('Location: /dashboard');
}
✓ Fixed
// Regenerate session ID after any privilege change — prevents session fixation
session_start();

$user = User::where('email', $email)->first();
if (!$user || !password_verify($password, $user->password)) abort(401);

// Regenerate BEFORE writing auth data
session_regenerate_id(true); // true = delete old session file

$_SESSION['user_id'] = $user->id;
$_SESSION['role']    = $user->role;

// On logout:
session_regenerate_id(true);
session_destroy();

// Also regenerate on privilege escalation:
// sudo, 2FA confirmation, admin impersonation, etc.

Added 15 Mar 2026
Edited 22 Mar 2026
Views 74
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 1 ping T 1 ping F 0 pings S 0 pings S 2 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 1 ping T 0 pings F
No pings yet today
Ahrefs 1
SEMrush 7 PetalBot 7 Amazonbot 6 Ahrefs 6 Perplexity 4 Bing 4 Google 3 Brave Search 3 Scrapy 2 Applebot 2 ChatGPT 1 Meta AI 1 Twitter/X 1
crawler 45 crawler_json 2
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
PHP php A server-side scripting language that generates web pages and APIs — the code runs on the server, and only its output (usually HTML or JSON) reaches the browser.

PHP is often the first server-side language people meet, and understanding its execution model — script starts fresh on every request, no memory between requests — explains most of how the web backend works: sessions, databases, and caching all exist to bridge that per-request amnesia.

💡 Start with PHP 8.x, declare(strict_types=1), and PDO — skip any tutorial that mentions mysql_query().

Ask Codex about PHP →
Session general A session is how a website remembers who you are as you click from page to page. The server stores your info under a unique ID, and your browser sends that ID back with every request.

Nearly every logged-in feature on the web depends on sessions, so understanding them is essential for building or securing anything with user accounts.

💡 Start the session first, store only an ID, and regenerate that ID whenever the user's privilege level changes.

Ask Codex about Session →
DEV INTEL Tools & Severity
🔴 Critical ⚙ Fix effort: Low
⚡ Quick Fix
Call session_regenerate_id(true) immediately after every successful login — the true parameter deletes the old session file, preventing session fixation attacks where an attacker pre-sets the session ID
📦 Applies To
PHP 5.0+ web
🔗 Prerequisites
🔍 Detection Hints
No session_regenerate_id() after login; session data written without regenerating ID; privilege escalation without new session ID
Auto-detectable: ✓ Yes semgrep phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: Function Tests: Update
CWE-384


✓ schema.org compliant