Prevent script from re-executing action on page refresh
I am building my own php cart for the past week and I got stuck in some issues.
I managed to add new items in the cart, and the URL looks like below.
http://blah-blah.com/order/index.php?action=add&id=84
The question is simple: How can I prevent from adding the same item again in the cart if som开发者_如何学运维eone refreshes the page? Cause now every time someone refreshes the page, it changes the quantity to +1 for the specific item..
Also, after moving to the checkout page, if they press the back button in the browser, AGAIN the quantity will change to +1;
Any recommendations?
Yea. Have them set, rather than increment the quantity.
Also, you'd usually use POST
(not GET
) requests for this sort of action. Browsers know this and cunningly ask the user whether they want to re-submit POST
data.
The cleanest approach for your user may be to do the whole thing with AJAX. If they go "back", they'll just go back to the last page they visited without trouble. This would be equivalent to how comments on Stack Overflow are submitted: you cannot go "back" to your submission step and end up writing duplicate comments.
I would suggest following steps to avoid multiple submission and back button issues:
- As suggested above you should use POST method to submit your form instead of GET.
- Prevent multiple form submission from the same session as described here: http://phpsense.com/php/prevent-duplicate-form-submission.html
After you're done with processing the POST request you should redirect to a confirmation URI OR better to the same URI as follows:
// redirect after processing the POST request
header("Location: " . $_SERVER["REQUEST_URI"]);
exit;
As said by Tomalak it's better to use POST datas for thoose kind of action. An other point you can use in conjunction with POST datas is to respond to the user with a redirect header then even if he try to refresh or go back the data won't be processed again.
basic example:
<?php
if( !empty($_POST) ){
// do stuff with POST datas then
header("location: mypage.php\n");
exit;
}
You can use
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
To redirect your user to the previous page? Pressing refresh or the back/forward buttons will not load the add page again.
I know this question is closed, But i have an alternative that I thought up for this exact problem.
The idea is to create a hash of $_POST data before and after. For this example i used md5, but you can use whatever algorithm you want:
$postHash = md5(serialize($_POST).date("dMY",time()));
if(!isset($_SESSION['post'])) $_SESSION['post']="Not yet set.";
Then all your processing code that would normally go at the top of the page you simply put into the following IF:
if($_SESSION['post']!=$postHash) {
// Perform actions
} else {
echo "Cannot post duplicate data";
}
Then, at the bottom of the page (or after processing has complete, and any database calls have been actioned):
$_SESSION['post'] = md5(serialize($_POST).date("dMY",time()));
There are still a few problems but it seems to work okay. Hope this helps.
Hi I don't have a coding solution for you but rather a logical solution to this problem give it a try it might help you, it helped me
I faced the same problem where I used the "ShoppingCart?AddToCart=1&....." in my URL(GET Variable) as a way of knowing that the user wants to add products to the cart and every time I refreshed the page the same product got added again and again
My way of working when I received the AddToCart Request was to first execute a method that will add the product to cart and then execute another method that will display the contents of the cart (THIS WAS THE PROBLEM RIGHT HERE)
I changed the logic a bit
Now whenever I receive the same AddToCart Request I execute the same first method to add the contents to the cart
BUT
Now rather than executing the second method to display the content of the cart I use the header("Location: ShoppingCart?ViewCart=1")
This means that after executing the first method which add the content to the cart I make a ViewCart Request to my Shopping Cart page, now every time I make a AddToCart Request it properly adds the contents to the cart and after that whenever I try to refresh the page to try to replicate the AddToCart Request I always end up with ViewCart Request and the Page simply shows the Cart contents and not add any futher contents to the cart
The trick over here is that every time I receive the "ShoppingCart?AddToCart=1&....." I execute the Add function and the header function in such a way that I end up with displaying "ShoppingCart?ViewCart=1" in the URL rather than the original "ShoppingCart?AddToCart=1&....."
Now even if some tries to refresh the page or comes back to the page using Back Button in the browser they always end making a "ShoppingCart?ViewCart=1" Request, they never see the "ShoppingCart?AddToCart=1&....." in the URL
like I said I don't have a Coding Solution but rather a Logical Solution
Hope this helps
精彩评论