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

Authentication

Security PHP 7.0+ Intermediate
debt(d8/e6/b7/t7)
d8 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'silent in production until users hit it' (d9), slightly better at d8 because SAST tools and security scanners (no detection_hints provided, but category-appropriate tools like Psalm taint analysis, SonarQube security rules) can flag weak hashing like MD5, but missing session_regenerate_id, missing rate limiting, and missing cookie flags are typically silent in production until exploited.

e6 Effort Remediation debt — work required to fix once spotted

Closest to 'cross-cutting refactor across the codebase' (e7), slightly better at e6 because while quick_fix lists single-call swaps (password_hash, session_regenerate_id), in practice fixing authentication touches the login flow, registration, password reset, session config, and rate limiting middleware — multiple files across the auth subsystem.

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

Closest to 'strong gravitational pull' (b7), because authentication applies across web and CLI contexts and every protected endpoint depends on the auth model; changing the hashing algorithm or session strategy ripples through the entire application.

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

Closest to 'serious trap' (t7), grounded in the misconception field: developers reasonably believe storing a user ID in $_SESSION is sufficient, but the working-but-exploitable nature (missing cookie_secure, cookie_httponly, session_regenerate_id, timeout) is a classic case where the obvious implementation looks correct and passes tests but is broken in adversarial conditions.

About DEBT scoring →

Also Known As

auth login user authentication sign in credential validation authn

TL;DR

The process of verifying that a user is who they claim to be — typically by validating credentials (password, token, certificate) and establishing a session or issuing a signed token for subsequent requests.

Explanation

Authentication answers 'who is this user?' — distinct from authorisation ('what can they do?'). PHP authentication typically uses one of three mechanisms: session-based (credentials validated, user ID stored in a server-side session, session ID sent as a cookie); token-based (JWT or opaque token issued on login, sent in Authorization header on subsequent requests, validated server-side); or OAuth/OIDC (delegated to a third party like Google, GitHub, or an identity provider). Critical implementation requirements: passwords must be hashed with password_hash() using PASSWORD_ARGON2ID or PASSWORD_BCRYPT (never MD5 or SHA); login forms must use HTTPS; brute-force protection (rate limiting, account lockout) must be in place; session IDs must be regenerated on privilege change (session_regenerate_id(true)) to prevent session fixation. Laravel Sanctum handles API token authentication; Laravel Breeze/Jetstream handle session-based authentication.

How It's Exploited

Weak authentication is exploited through credential stuffing (automated login attempts using leaked username/password pairs), brute force attacks (systematic guessing), phishing (tricking users into revealing credentials), session hijacking (stealing unencrypted session IDs), token theft (intercepting JWTs in transit or from localStorage), and exploitation of misconfigured OAuth flows. Common implementation flaws exploited include passwords hashed with weak algorithms (MD5, unsalted SHA), missing HTTPS on login forms, lack of rate limiting on login endpoints, and failure to regenerate session IDs after privilege escalation. Attackers also exploit overly permissive CORS policies to steal tokens, or target client-side storage (localStorage) containing tokens vulnerable to XSS.

Watch Out

⚠ Session cookies set with httpOnly and secure flags can still leak the session ID if HTTPS is not enforced site-wide — an attacker on a man-in-the-middle can downgrade the initial login request to HTTP, intercept the session cookie being set, then replay it over HTTPS. Always use HSTS headers and redirect all HTTP traffic to HTTPS before the login form is served.

Common Misconception

✗ Storing the user ID in a session is all that is needed for authentication. Session-based authentication requires several additional security controls: the session must be started over HTTPS only (session.cookie_secure = true), the session cookie must be HTTP-only (session.cookie_httponly = true) to prevent JavaScript access, the session ID must be regenerated after login to prevent session fixation attacks, and the session must have a reasonable timeout. Missing any of these produces a working but exploitable authentication system.

Why It Matters

Authentication is the gateway to every protected resource in a PHP application. Mistakes in authentication implementation — weak password hashing, missing session regeneration, no brute-force protection, HTTP-only cookies absent — translate directly into account takeovers, data breaches, and credential stuffing attacks. The most common PHP authentication vulnerability is not using prepared statements in the credential check query (SQL injection bypass) or using MD5 for password hashing (crackable in seconds on a GPU). Both are easily avoided with password_hash() and PDO.

Common Mistakes

  • Using MD5 or SHA-1 for password hashing — use password_hash($pass, PASSWORD_ARGON2ID) exclusively.
  • Not regenerating the session ID after login — session_regenerate_id(true) prevents session fixation attacks.
  • Missing brute-force protection — without rate limiting, an attacker can try millions of passwords against any login endpoint.
  • Storing plaintext passwords or reversibly encrypted passwords — if the database is compromised, all passwords are exposed immediately.

Avoid When

  • Do not use authentication to control what a user can see or do — that is authorization; authentication only verifies identity.
  • Do not rely on authentication alone to protect sensitive data in transit — use HTTPS encryption regardless of authentication mechanism.
  • Do not implement custom authentication schemes instead of proven libraries (Laravel Sanctum, Passport, or standard OAuth/OIDC providers) — roll-your-own authentication is a common source of vulnerabilities.
  • Do not authenticate on every micro-interaction (e.g., each keystroke or scroll) — authenticate once at login and use session/token validation for subsequent requests.

When To Use

  • You need to verify user identity before granting access to protected resources, user accounts, or sensitive operations in your application.
  • You're building an API and must validate that each request comes from an authorized client, whether via session cookies, bearer tokens, or OAuth credentials.
  • You're integrating with third-party services (Google, GitHub, Azure) and want to offload identity verification to a trusted provider via OAuth/OIDC.
  • You need to enforce role-based or permission-based access control and must first establish *who* the user is before checking *what* they can do.

Code Examples

✗ Vulnerable
// MD5 hashing + no session regeneration + SQL injection
$hash = md5($_POST['password']); // crackable in seconds
$user = $db->query("SELECT * FROM users WHERE email='{$_POST['email']}'
    AND password='$hash'"); // SQL injectable
if ($user) {
    $_SESSION['user_id'] = $user['id'];
    // No session_regenerate_id — session fixation possible
}
✓ Fixed
// Secure authentication
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?');
$stmt->execute([$_POST['email']]);
$user = $stmt->fetch();

if ($user && password_verify($_POST['password'], $user['password_hash'])) {
    session_regenerate_id(true); // prevent session fixation
    $_SESSION['user_id'] = $user['id'];
    $_SESSION['logged_in_at'] = time();
    // Redirect — never re-display login form on success
    header('Location: /dashboard');
    exit;
}

Added 23 Mar 2026
Edited 25 Jun 2026
Views 207
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 3 pings F 0 pings S 1 ping S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 19 PetalBot 17 Applebot 12 Ahrefs 11 SEMrush 11 Perplexity 10 Scrapy 9 Google 7 Bing 7 Claude 4 Meta AI 2 Twitter/X 2 Brave Search 2 Baidu 2 ChatGPT 1
crawler 114 crawler_json 2
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
Password security A password is a secret string a user types to prove they are who they claim to be. In security work, the important part isn't the password itself — it's how your app stores and checks it.

Passwords are still the front door of almost every app, so mistakes here leak entire user bases at once. Storing them correctly is one of the highest-impact security habits a developer builds.

💡 Never store or compare passwords directly — always hash on save with password_hash and check on login with password_verify.

Ask Codex about Password →
Permission security A permission is a rule that says who is allowed to do what with a resource, like read a file, edit a record, or delete an account.

Almost every security problem eventually traces back to a missing or wrong permission check. Getting permissions right is how you keep private data private and prevent one user from harming another.

💡 Every sensitive action needs a server-side permission check — never trust the UI alone.

Ask Codex about Permission →
Two-Factor Authentication (2FA) security Two-factor authentication requires two different types of proof to verify your identity—typically something you know (password) plus something you have (phone) or something you are (fingerprint).

Passwords get stolen constantly through phishing, breaches, and weak choices. 2FA means a stolen password alone isn't enough—attackers need physical access to your phone or hardware key, dramatically reducing successful account compromises.

💡 Enable 2FA on every account that offers it, and prefer authenticator apps over SMS whenever possible.

Ask Codex about Two-Factor Authentication (2FA) →
Vulnerability security A vulnerability is a weakness in software, hardware, or processes that an attacker could exploit to cause harm, steal data, or gain unauthorized access.

Every breach starts with a vulnerability. Understanding what makes software weak is the first step to writing code that resists attack and protecting the systems you build.

💡 Treat every input as potentially hostile—validate, sanitize, and use prepared statements before trusting any data from users or external systems.

Ask Codex about Vulnerability →
⚡ DEV INTEL Tools & Severity
🔴 Critical ⚙ Fix effort: Medium
⚡ Quick Fix
Use password_hash($pass, PASSWORD_ARGON2ID) to hash, password_verify($input, $hash) to check, session_regenerate_id(true) after successful login, and rate-limit login attempts
📦 Applies To
PHP 7.0+ web cli
🔗 Prerequisites


✓ schema.org compliant