简体   繁体   中英

prevent double execution of code in PHP

I have a store which works like this:

  1. The user adds stuff to his cart on the 1 st page
  2. The user submits the cart ( POST ), all post data is stored in the session.
  3. The user fills out the form on the 2 nd page, clicks submit again (second POST )
  4. The user comes at the "thank you" page (3 rd ), which takes all the session data, and the form POST s the data and mails it.

The problem is, a simple refresh causes an empty table to be sent per email. How can I prevent this? Can I redirect somehow after the execution of the mail function? So the thank you page doesn't do anything on refresh?

You can do by calling the PHP header() function like this:

header('Location: thankyou.php');
exit;

The user will be redirected to the thankyou page and a reload there will not execute your code again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM