How can PHP sessions be used to prevent a form from being submitted multiple times?

To prevent a form from being submitted multiple times, you can use PHP sessions to set a flag when the form is submitted and then check this flag before processing the form. If the flag is set, you can prevent the form from being submitted again.

<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (!isset($_SESSION['form_submitted'])) {
        // Process the form data
        $_SESSION['form_submitted'] = true;
    } else {
        echo "Form has already been submitted.";
    }
}
?>