// // Usage on a POST handler (first line, before reading $_POST): // csrf_check(); // // The token is one-per-session (Delight\Auth's session). It rotates on // logout because the PHP session is destroyed. State-changing GETs are // not protected (and shouldn't exist — see logout.php for the POST flip). declare(strict_types=1); function csrf_token(): string { if (session_status() !== PHP_SESSION_ACTIVE) { // Defensive: bootstrap.php always starts the session, but if a // caller forgot, do it here so the token can be stored somewhere. session_start(); } if (empty($_SESSION['csrf_token']) || !is_string($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } return $_SESSION['csrf_token']; } /** * Render a hidden form input carrying the current session's CSRF token. * Use this inside every POST