Prevent the script from re-executing the action when refreshing the page

I create my own php cart in the last week and I am stuck with some problems.

I managed to add new items to the cart and the url is as follows.

http://blah-blah.com/order/index.php?action=add&id=84 

The question is simple: how can I prevent the same item from being added to the cart? if someone is refreshing the page? The reason is now every time someone refreshes the page, he changes the quantity by +1 for a certain element.

In addition, after going to the verification page, if they click the "Back" button in the browser, "AGAIN" the number will change by +1;

Any recommendations?

+4
source share
6 answers

Yes. Install them, not increase the number.

In addition, you usually use POST (not GET ) requests for this kind of action. Browsers know this and slyly ask the user if they want to resend the POST data.

The cleanest approach for your user could be to do everything with AJAX. If they return, they will return to the last page they visited without any problems. This will be equivalent to how comments are sent to the stack overflow: you cannot return to your sending step and as a result write duplicate comments.

+4
source

I would suggest the following steps to avoid multiple problems with submit and back button:

  • As suggested above, you should use the POST method to submit your form instead of GET.
  • Prevent submitting multiple forms from the same session as described here: http://phpsense.com/php/prevent-duplicate-form-submission.html
  • After you finish processing the POST request, you should redirect confirmations to the URI or better to the same URI as below:

    // redirect after processing the POST request
    header ("Location:". $ _SERVER ["REQUEST_URI"])

    output;

+2
source

As Tomalak said, it is better to use POST data for this kind of action. Another point you can use with POST data is the response to the user with the redirect header, and even if he tries to update or return, the data will not be processed again.

basic example:

 <?php if( !empty($_POST) ){ // do stuff with POST datas then header("location: mypage.php\n"); exit; } 
0
source

you can use

 <?php header("Location: http://www.example.com/"); /* Redirect browser */ 

To redirect the user to the previous page? Pressing the refresh button or the back / forward button will not load the add page.

0
source

I know this question is closed, But I have an alternative that I came up with for this exact problem.

The idea is to create a $ _POST data hash before and after. In this example, I used md5, but you can use any algorithm you want:

 $postHash = md5(serialize($_POST).date("dMY",time())); if(!isset($_SESSION['post'])) $_SESSION['post']="Not yet set."; 

Then, all of your processing code, which is usually located at the top of the page, you simply type in 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 and any database calls have completed):

 $_SESSION['post'] = md5(serialize($_POST).date("dMY",time())); 

There are a few more problems, but everything seems to be in order. Hope this helps.

0
source

Hi, I don’t have a coding solution for you, but rather a logical solution . Try it, it can help you, it helped me

I had the same problem when I used "ShoppingCart? AddToCart = 1 & ....." in my URL (GET Variable) as a way to find out that the user wants to add products to the cart, and every time I refresh the page , the same product was added again and again

My way of working, when I received the AddToCart request , was the first to execute a method that would add the product to the basket and then execute another method that would display the contents of the basket (THIS WAS A PROBLEM RIGHT HERE)

I changed the logic a bit

Now that I get the same AddToCart request , I execute the same first method to add content to the cart

BUT

Now, instead of the second way to display the contents of the basket, I use the heading ("Location: ShoppingCart? ViewCart = 1")

This means that after executing the first method that adds content to the cart, I make a ViewCart Request on the page of my shopping cart, now every time I make an AddToCart request> it correctly adds the contents to the cart, and after that, when I try refresh the page to try to replicate the AddToCart request , I always get a ViewCart request, and the Page just shows the contents of the cart, and does not add more content to the cart.

The trick here is that every time I get "ShoppingCart? AddToCart = 1 & ....." , I execute the Add function and the header function so that I end up with the display of "ShoppingCart? ViewCart = 1" in URL, not in the original "ShoppingCart? AddToCart = 1 & ....."

Now, even if some people try to refresh the page or return to the page using the "Back" button in the browser, they always end the query "ShoppingCart? ViewCart = 1" , they never see "ShoppingCart? AddToCart = 1 & ....." in url

as I said, I don't have a coding solution, but rather from a logical solution

Hope this helps

-1
source

All Articles