oursolutionarchitectoursolutionarchitect

PHP PRG (Post-Redirect-Get)


Summary: in this tutorial, you’ll learn how to use the PHP PRG (Post-Redirect-Get) technique to prevent the double form submission problem.

The double submit problem

To change data on the server via a form, you often use the post method. When a form is submitted, you validate the data, update the database, and display the output.

However, if you click the Refresh (or Reload) button of the browser after the form is submitted, the browser will submit the form again.

Since the form uses the POST method, the browser will prompt for confirmation like this:

PHP PRG - Form Resubmission Problem

If you click the Continue button, the browser will actually submit the form a second time. It’s a notorious double submit problem that may cause many issues.

For example, you may have duplicate records in the database. If the form processes the payment, it’ll charge the customer twice.

The following diagram illustrates the double submit problem:

Introduction to the PHP PRG (post-redirect-get) technique

The PRG (post-redirect-get) technique helps you solve the double submit problem. The post-redirect-get works as follows:

  • First, the form is submitted using the HTTP POST method.
  • Second, the server does something, e.g., updates the database, processes a payment, sends an email, and redirects the browser to the result page.
  • Third, the browser makes the second request to the result page using HTTP GET method.
  • Finally, the server returns the result page and the browser displays it.

If the user refreshes the web browser, the browser will request the result page using the HTTP GET method.

Note that the result page and the page that contains the form can be the same.

The following diagram illustrates how the PRG works: