How to prevent data forwarding when updating(F5) in PHP?

Asked

Viewed 1,057 times

0

Good evening guys, I am creating an online game where the user has the option to use an item to recover HP. When you click consume, an Alert appears on the screen stating that the item has been consumed and that HP has been recovered.

The problem, as you can imagine, is that every time I hit F5 the HP keeps increasing and the Alert appears again. I tried to use unset($_POST['']) to clean what was chosen by the user but did not work.

Thanks in advance for the help.

  • Ideally, a POST request should never have an HTML page as an answer, precisely because of this problem. The correct thing would be for you to set the header Location and redirect the customer back to the original page.

  • It is that this requisition happens in the "main page" of the game: tavern.php. Consuming the item that recovers HP is one of the functions available on this page, so ideally after consuming the item the user stays on that page, understand?

  • This doesn’t change anything, you can redirect it to yourself, but no post.

  • Can you tell me how to do that?

  • Exactly, making the Location for the same page, the client will make a GET request and not a POST.

  • There are 5 links at the close of the question, there are more detailed what you need.

  • Okay, thanks for the personal help, I think I get it

  • It would be even better to send it to a "get.php" that redirects to the "tavern.php" back, so it confuses the code less. (the user will not see this "gets.php" pq will already be sent back with header( 'Location: /recebe.php');die();

  • An empty page that would only have a redirect to the main page?

  • Exactly, this would only have PHP to save to DB or process the data, and the Location header. Read this link here, it may help if you find a headers error: https://answall.com/questions/4251/70

  • This mistake was the first I found when I started, I created my account here just to find out how to solve it hahahahaha. Thank you so much for your help, I understand what I have to do

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.