How can one prevent data from being resubmitted when a form is refreshed in PHP?

When a form is submitted in PHP and the user refreshes the page, the data can be resubmitted, causing duplicate entries or unintended actions. To prevent this, you can use the POST/Redirect/GET pattern. After processing the form data, redirect the user to another page using the header() function. This way, if the user refreshes the page, only the redirected page will be loaded without resubmitting the form data.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process form data
    
    // Redirect to another page to prevent form resubmission
    header("Location: success.php");
    exit();
}
?>