Flash messages (show once)
Store a message in the session before a redirect and show it once on the next page.
PHP · Flash messages (show once)Beginner
function flash(?string $msg = null): ?string
{
if ($msg !== null) { $_SESSION['flash'] = $msg; return null; }
$m = $_SESSION['flash'] ?? null;
unset($_SESSION['flash']);
return $m;
}
// After saving:
flash('Saved successfully.');
header('Location: /list.php');
exit;
// On the next page:
if ($m = flash()) echo '<p class="notice">' . htmlspecialchars($m, ENT_QUOTES, 'UTF-8') . '</p>';sessionmessagesux