How can I prevent multiple submissions of a form in PHP?

To prevent multiple submissions of a form in PHP, you can use session variables to track whether the form has already been submitted. Set a session variable when the form is submitted and check for this variable before processing the form data. If the variable is already set, do not process the form again.

session_start();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (!isset($_SESSION['form_submitted'])) {
        // Process the form data
        // ...
        
        // Set session variable to prevent multiple submissions
        $_SESSION['form_submitted'] = true;
    } else {
        // Form has already been submitted
        echo "Form has already been submitted";
    }
}