Prevent php form resubmission by ip. How can I prevent a form from resending a message? Preventing form resubmission using server redirect


Let's say we are making a script that accepts data from a form submitted using the POST method. The script received the data, processed it and displayed a page with the result. But if the user decides to refresh the page at this moment, he will see a message like this:

To display this page, Firefox must send information that will repeat any previously performed action (for example, a search request or an online purchase).

And two buttons. Clicking on one of them will resend the data, which is often undesirable. Clicking the second one will not refresh the page. In any case, the user does not feel good about such a message. Users generally don’t really like all sorts of pop-up windows.

To begin with, I’ll show you the script that we will be finalizing.

You can submit the form once, and then press Ctrl+R and see the ill-fated window. Let's get rid of it.

But first, a word from the sponsor of the post - a site with useful content for Samsung phones, which offers themes for the Samsung gt s5230, wallpapers and other stuff.

Preventing form resubmission using server redirect

To prevent form data from being sent again, you can do a server redirect. This is done by sending the browser a Location header with the desired URL. For example, this should be a thank you page for completing the form. Then we will write something like:

In this case, the server will receive the data, process it, and instead of showing the result, send the client to a page where this result will be shown.

The disadvantage of this method is that the user may click the back button and return to the redirected page. She will throw it forward again and so the user will hardly be able to return two pages back to the form that he originally completed.

Preventing form resubmissions using a client-side redirect

A client redirect is called client redirect because it happens on the client side. That is, in the browser. Client redirect can occur using JavaScript and meta tags.

JavaScript has the advantage of overwriting the browser's History so that even if the user clicks the browser's back button, they will not return to the page that the form handler returned. That is, the window will completely disappear. But some people have JS disabled.

META tags, on the other hand, have the advantage of being versatile. They redirect everyone, always.

It would be optimal to combine these two methods. How - Alexander Shurkayev described the optimal redirect in his note.

We use his method as follows.

Let's try! Now, as you can see, no window appears. What have we done? We checked. If the data has been received, we display everything necessary for the redirect. In principle, after this you can even exit, so as not to load the browser with unnecessary data that no one will see anyway.

The HTML form submission process may take several seconds before the form is successfully submitted and the response page is displayed. Users who are not patient enough may click the Submit button multiple times, causing the form to be resubmitted. This is usually not a problem, but in some cases you can do something to prevent it from happening.
Below you will find two simple tricks to prevent double messaging, you can use any of these or a combination of them.

Preventing multiple submissions with Javascript

Probably using Javascript to prevent the form from being resubmitted is the easiest way. When the user submits the form, you can disable the “Submit” button and change its name to something more clear: “Submitting, please wait...”

The first step is to set a unique id , for example id="myButton":

The second (and final) step is to specify two Javascript commands in the . The first command will disable the Submit button after the form is submitted, and the second will change the button text to give the user an idea of ​​what's happening. The following code needs to be added to the tag:

The form tag will look like this:







2024 gtavrl.ru.