当前位置: 动力学知识库 > 问答 > 编程问答 >

redirect - 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 1st 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 2nd page, clicks submit again (second POST)
  4. The user comes at the "thank you" page (3rd), which takes all the session data, and the form POSTs 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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: