At one time, I was puzzled by the question - how to protect the pages of the site from re-sending form data during the page refresh (if it was before, of course, sending).
Each webmaster and developer probably knows that if you clicked on the “send” button on the website, filling out a form, then after sending, if you try to refresh the page, the browser will display a message confirming the resubmission.
In some moments it is unacceptable. For example, in the case of an elementary feedback form. When the user filled out the form and sent a message, and then for some reason known to him, he updated the page, the letter went away again. This may, of course, not be such a fatal case, just as an example. Everything is much more painful, for example, when sending an order in an online store.
So, I asked myself the question of finding a solution to this problem and realized that there is only one solution: use redirection after sending the form header ('location: address'). Those. everything is simple - after sending we call redirection (you can even go to the same page) and that's it! Updating the page will be clean, without any filled POST-s and GET-s.
Everything would be fine, but I personally experienced some problems with it. They are as follows. Previously, without using redirection, the mechanism for sending data on my websites worked as follows:
The user fills out a form, presses “send”, the script accepts the sent data, checks them for correctness (validity, required data, etc.) and gives an answer - either the operation was successful, or an error occurred and a list of errors (for example: error - The “name” field is not filled in. On the sending page, the corresponding message is already displayed: sending is successful or not successful.
If the submission is not successful, the form remains on the screen and its fields are filled with the data that the user entered. Those. the data is taken from the $ _POST variable (if the POST method) and get into the appropriate fields (that is, they are returned from the post to their own fields in order not to enter them again). After all, it’s annoying everyone when you filled out the form, and God forbid something indicated incorrectly and when you try to send you a message is displayed that something is filled in incorrectly and the form is updated and empty again. And it is necessary because of one incorrectly filled field to re-fill it.
So, as already said, in case of unsuccessful completion, the form remains filled with data taken from $ _POST, and the user can correct the incorrect data and send the form again.
Everything was good and everything worked.
But then I made the sending using redirection and it turned out that after clicking on the “send” button, in case of unsuccessful filling, the form was updated and the filled fields could no longer be left in it, because earlier they were automatically filled from the $ _POST array, and now after the redirection occurred $ _POST was cleaned up as if there was no sending.
But in this case there was a way out. Use session. Those. before calling the header, transfer to the session variables the data from the POST and only then, after the redirection, to operate with them.
As a result, the code became more complicated. Debugging complicated because it is generally difficult to determine what happens to functions at the moment redirection happens. If you made some mistake in the codes (which appears at the moment of sending), then it will not even be displayed, since redirection will occur and you will not even see the error message.
In general, after introducing header into my codes, it became more difficult for me to work with my applications. Development / revision / search errors is complicated. But I can not refuse it.
And I continue to ask myself: are there any other, more elegant solutions?