Skip to content
PHP Logo

How can the Post-Redirect-Get pattern be implemented in PHP to prevent form resubmission on page refresh?

When a form is submitted in PHP and the user refreshes the page, the form data is resubmitted, causing duplicate entries or unwanted actions. To prevent this, the Post-Redirect-Get pattern can be implemented. After processing the form data, redirect the user to another page using the header function to prevent resubmission on page refresh.

php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Process form data

    // Redirect to another page to prevent form resubmission
    header("Location: success_page.php");
    exit;
}