note 90174 deleted from function.header by danbrown
| From: | danbrown@php.net | Date: | Wed, 08 Apr 2009 23:11:49 +0000 |
| Subject: | note 90174 deleted from function.header by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152984@lists.php.net to get a copy of this message | ||
Note Submitter: c t u b b s i i - g - m a i l - c o m
----
To prevent users from submitting POST data twice (or being prompted to) when refreshing or hitting
the back button:
A Shopping Cart Example: shoppingcart.php
<?php
ob_start();
session_start();
if (isset($_POST['update'])) { // an 'update quantity' form submit button
// update shopping cart from POST data
header('Location: shoppingcart.php'); // reload cart without POST data
ob_end_flush();
exit();
}
// display shopping cart here, including:
// <form method="post" action="shoppingcart.php"> </form>
// for this example, it would make sense to have a button like:
// <input type="submit" name="update" value="Update Quantities"
/>
// and quantity fields for cart items (obviously)
?>
Run through:
1. View the shopping cart by navigating to shoppingcart.php.
2. Update the quantities of your items and click "Update Quantities".
3. The POST data is processed by shoppingcart.php (same page in this example).
4. The user is redirected to shoppingcart.php without POST data.
5. Refresh the page or navigate to another page and go back (as long as you don't go back
twice).
6. You will see the page as intended without re-processing the POST data.
The technique can be extended to include a session variable noting that the page has already been
submitted, or whatever. It also doesn't have to be the same page, as long as the page that
processes the POST data redirects to one that doesn't without propagating the POST data.