๐ŸŒ PHP Web Development
Estimated reading: 3 minutes 27 views

๐Ÿ” PHP Postโ€“Redirectโ€“Get (PRG) โ€“ Prevent Duplicate Form Submissions

Implement the PRG pattern in PHP to enhance UX and avoid form resubmission on page refresh.


๐Ÿงฒ Introduction โ€“ Why PRG Matters in PHP Forms

If a user submits a form and then refreshes the page, most browsers will warn that resubmitting the form could lead to duplicate entries. This happens when a form submitted via POST is re-sent on refresh. The Postโ€“Redirectโ€“Get (PRG) pattern is a proven solution to this common problem.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • What PRG is and why it’s useful
  • How to implement PRG in PHP
  • How PRG improves UX and data consistency
  • Best practices for using PRG in form handling

๐Ÿ” PHP Postโ€“Redirectโ€“Get (PRG)

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Process form data here
    header("Location: thank-you.php");
    exit;
}

โžก๏ธ PRG prevents form resubmission if the user refreshes the page after a POST request.
โžก๏ธ Use header("Location: ...") after processing and before outputting anything.


๐Ÿงช How PRG Works โ€“ Step-by-Step

  1. POST: User submits a form โ†’ PHP processes the data
  2. Redirect: PHP sends a Location: header to a new page (GET request)
  3. GET: User lands on a new URL with a fresh state (no form data)

โœ… PRG Implementation Example

๐Ÿ”น Form Page (form.php)

<form action="form.php" method="post">
  <input name="username">
  <input type="submit" value="Submit">
</form>

๐Ÿ”น PRG Logic (form.php)

session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $_SESSION['flash'] = "Form submitted successfully!";
    header("Location: form.php");
    exit;
}

if (isset($_SESSION['flash'])) {
    echo "<p>" . $_SESSION['flash'] . "</p>";
    unset($_SESSION['flash']);
}

โœ… This pattern:

  • Processes the form
  • Redirects the user
  • Displays a one-time flash message

๐Ÿ” Why Use PRG?

BenefitDescription
๐Ÿ›ก๏ธ Prevents duplicate submissionsNo repeat insertions if the user refreshes the page
๐Ÿ“Ž Clean URL behaviorEnds on a GET request with no visible POST data
๐Ÿ’ฌ Allows flash messagingTemporarily store success/failure messages in session

๐Ÿšซ Common Mistakes to Avoid

  • โŒ Echoing output before calling header("Location:")
  • โŒ Not calling exit after redirect โ€” it can cause unexpected behavior
  • โŒ Forgetting to unset session flash messages after displaying

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

The PRG pattern is a best practice in PHP development, especially for handling form submissions. It enhances UX, prevents duplicate data, and enables flash messages for better user feedback.

๐Ÿ” Key Takeaways:

  • PRG = Post โ†’ Redirect โ†’ Get
  • Use it to prevent form resubmission on refresh
  • Combine with sessions for one-time flash messages
  • Always call exit after header("Location:")

โš™๏ธ Real-World Use Cases:
User registration, contact forms, checkout flows, login redirects, profile updates


โ“ Frequently Asked Questions (FAQs)

โ“ What does PRG stand for in PHP?
โœ… Postโ€“Redirectโ€“Get. It’s a design pattern used to prevent duplicate form submissions.

โ“ Why use header("Location: ...") after POST?
โœ… It sends the browser to a new page, clearing POST data from memory to prevent re-submission.

โ“ Do I need to call exit after redirect?
โœ… Yes. To stop PHP from continuing script execution after the header is sent.

โ“ Can I use PRG with flash messages?
โœ… Absolutely. Store temporary messages in $_SESSION and display them after redirect.

โ“ Is PRG useful for AJAX forms?
โš ๏ธ Not directly. PRG is for traditional page-based forms. Use JSON response handling for AJAX.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ” PHP Postโ€”Redirectโ€”Get (PRG)

Or Copy Link

CONTENTS
Scroll to Top