// Start the session session_start(); // Check if the form was submitted if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Process the form data // ... // Set a flash message $_SESSION['flash'] = 'Your form has been submitted successfully!'; // Redirect to a thank you page header('Location: thankyou.php'); exit; } // In the thankyou.php page, display the flash message session_start(); if (isset($_SESSION['flash'])) { echo '' . $_SESSION['flash'] . ''; unset($_SESSION['flash']); }
// Start the session session_start(); // Check if the login form was submitted if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Check the login credentials if ($username === 'admin' && $password === 'secret') { // Login successful $_SESSION['user'] = $username; header('Location: dashboard.php'); exit; } else { // Login failed $_SESSION['flash'] = 'Incorrect username or password'; } } // In the login page, display the flash message if it is set session_start(); if (isset($_SESSION['flash'])) { echo 'In this example, we start the session and check if the login form was submitted. If the login credentials are correct, we set the `$_SESSION['user']` variable and redirect the user to the dashboard page. If the credentials are incorrect, we set a flash message in the session and display it in the login page. We start the session again and use the `isset()` function to check if the flash message is set. If it is, we display it and unset it to ensure it is only shown once. Package library used in this examples - "symfony/http-foundation".' . $_SESSION['flash'] . ''; unset($_SESSION['flash']); }