prepare("SELECT * FROM users WHERE id=?"); $st->execute([$_SESSION["user_id"]]); return $st->fetch(); } return null; } function is_login() { return !empty($_SESSION["user_id"]); } function is_admin() { $u = current_user(); return $u && $u["role"] === "admin"; } function require_admin() { if (!is_admin()) { header("Location: " . SITE_URL . "login.php"); exit; } } function attempt_login($username, $password) { $st = db()->prepare("SELECT * FROM users WHERE username=?"); $st->execute([$username]); $user = $st->fetch(); if ($user && password_verify($password, $user["password_hash"])) { $_SESSION["user_id"] = $user["id"]; return true; } return false; } function logout() { unset($_SESSION["user_id"]); }