How can sessions be utilized to address the issue of page reloads in PHP?

When a page is reloaded in PHP, it can cause unwanted actions to be repeated or data to be resubmitted. One way to address this issue is by using sessions to store a flag or token that indicates whether a form has already been submitted. By checking this flag before processing the form data, you can prevent duplicate submissions.

session_start();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (!isset($_SESSION['form_submitted'])) {
        // Process form data
        $_SESSION['form_submitted'] = true;
    } else {
        // Redirect or display an error message to prevent duplicate submissions
        header('Location: /form.php');
        exit();
    }
}