Duplicate form submission is one of the most common and troublesome problems in multi-user Web applications. There are many scenarios where a recurring submission problem is encountered, such as:
Click the Submit button two times.
Click the Refresh button.
Use the browser Back button to repeat the previous action, causing the form to be submitted repeatedly.
Repeat the form using the browser history.
The HTTP request that the browser repeats.
This is a tricky issue when users submit a form because of the speed of the network or if the Web page is maliciously refreshed, causing the same record to be repeatedly inserted into the database. We can start with the client and server side to try to avoid duplicate submissions of the same form. The Guest house below collects 8 common ways to effectively prevent form recurrence: source www.bkjia.com
1, JS ban the submit button.
Use JavaScript to make the submit button disable after the form is submitted. This method prevents impatient users from clicking the button multiple times. There is a problem, however, that this approach is ineffective if the client disables JavaScript.
My previous article has said that using some jquery plug-ins works fine. Reference: JS prevent form Repeat submission method and code
2, the use of Post/redirect/get mode.
Perform page redirection after submission, which is called the Post-redirect-get (PRG) mode. In short, when the user submits the form, you perform a client redirect and go to the Submit Success Information page.
This avoids repeated submissions caused by F5, and it does not warn of repeated submission of browser forms, and eliminates the same problem of advancing and backing up by browsers.
3, in the session to store a special logo.
On the server side, generates a unique identifier, stores it in session, writes it to the form's hidden fields, and then sends the form page to the browser, clicks on the submission, and, on the server side, gets the value of the hidden field in the form, compared to the unique identifier in the session, The equality note is the first commit, the request is processed, and the unique identifier in the session is removed; The unequal description is a duplicate commit and is no longer processed.
This gives your Web application a higher level of XSRF protection.
Please see the following code:
<?php
session_start ();
Generates a random number
$code = Mt_rand (0,1000000) based on the current session;
$_session[' Code ' = $code;
? >
passing random numbers as hidden values on the page form, the code is as follows:
<input type= "hidden" name= "originator" value= "<?= $code?>" >
in the receiving page the PHP code is as follows:
<?php
session_start ();
if (Isset ($_post[' originator ')) {
if ($_post[' originator '] = = $_session[' Code ']) {
//Process the statement of the form, omit
} else{
Echo ' Please do not refresh this page or repeat the form. ';
}
}
? >
4. Using the header function to turn
In addition to the above method, there is a simpler way, that is, when the user submits the form, server-side processing immediately to the other pages, the code is as follows.
if (Isset ($_post[' action ')) && $_post[' action '] = = ' submitted ') {
Working with data, such as inserting data, and immediately moving to another page
Header (' location:submits_success.php ');
}
This way, even if the user uses the Refresh key, it will not cause the form to be duplicated because it has moved to the new page, and the page script has ignored any submitted data.
5. Processing of form expiration
In the development process, often appear the form error and return the page when the information is all lost, in order to support page bounce, you can use the following two ways to achieve.
1. Use header header to set cache control header Cache-control.
Header (' Cache-control:private, Must-revalidate '); Support page Bounce
2. Use the Session_cache_limiter method.
Session_cache_limiter (' Private, must-revalidate '); To write before the Session_Start method
The following code fragment prevents users from filling out a form, and when you click the Submit button to return, the content you just filled out on the form is not cleared:
Session_cache_limiter (' NoCache ');
Session_cache_limiter (' private ');
Session_cache_limiter (' public ');
Session_Start ();
The following is the contents of the form, so that when the user returns the form, the content that has been filled out will not be emptied
Attach the code to the top of the script you want to apply. Cache-control message header field Description
CACHE-CONTROL Specifies the caching mechanism that the request and response follow. Setting Cache-control in a request message or in a response message does not modify the caching process during another message handling process.
The cache directives requested include No-cache, No-store, Max-age, Max-stale, Min-fresh, and only-if-cached, and the instructions in the response message include public, private, No-cache, No-store, No-transform, Must-revalidate, Proxy-revalidate and Max-age. The directive meaning in each message is shown in table 5-3.
Table 5-3
|
Caching directives |
Description |
|
Public |
Indicates that the response can be cached by any buffer |
|
Private |
Indicates that the entire or partial response message for a single user cannot be handled by the shared cache. This allows the server to simply describe a partial response message from a user that is not valid for another user's request |
|
No-cache |
Indicates that a request or response message cannot be cached |
|
No-store |
Used to prevent important information from being inadvertently released. Sending in a request message will make neither the request nor the response message use the cache |
|
Max-age |
Indicates that the client can receive a response that is no longer than the specified time (in seconds) |
|
Min-fresh |
Indicates that the client can receive response times that are less than the current time plus a specified time |
|
Max-stale |
Indicates that the client can receive response messages that exceed the timeout period. If you specify a value for the Max-stale message, the client can receive a response message that exceeds the specified value for the timeout period |
For an introduction to sessions and cookies, see Chapter 10th "PHP Session Management" For more information.
6. Tips for judging form movements
A form can assign the action that should be handled by the same program, there are different logic in the form, how to judge the button content that the user presses is just a small problem.
In fact, as long as the name of the submit button can be known, when the form is submitted, only the pressed button of the submit type will be sent to the form singular, so as long as the value of the button is available to know which button the user presses, take the following form as an example:
<form method= "POST" action=test.php>
<input type=submit name= "btn" value= "a" >
<input type=submit name= "btn" value= "B" >
</FORM>
When the user presses the "a" button btn=a, press the "B" button, then btn=b.
You can also use the name of the Submit button (name) to determine, see the following code:
<form method= "POST" action=test.php>
<input type=submit name= "a" value= "Submit a" >
<input type=submit name= "B" value= "Submit B" >
</FORM>
So as long as the post/get parameters have a or B, you can know which button is pressed.
<?php
Print_r ($_post);
?>
7, add constraints in the database.
Add a unique constraint to the database or create a unique index to prevent duplicate data from appearing. This is the most effective way to prevent duplicate submission of data.
How do you overcome the problem of repetitive data submission? Do you have any real-world examples of repetitive data submissions?
Reproduced from: http://www.bkjia.com/jingyan/471187.html
5. Using Client Script
Referring to client-side scripting, JavaScript is often used for general input validation. In the following example, we use it to handle the recurrence of a form, see the following code:
<form method= "POST" name= "register" action= "test.php" enctype= "Multipart/form-data" >
<input name= "text" type= "text" id= "text"/>
<input name= "cont" value= "Submit" type= "button" onclick= "Document.register.cont.value=" is being submitted, please wait ... '; Document.register.cont.disabled=true;document.the_form.submit (); " >
</form>
When the user clicks the Submit button, the button changes to a gray unavailable state.
The above example uses the OnClick event to detect the submission status of the user, and if the "submit" button is clicked, the button is immediately disabled and the user cannot click the button to submit again.
8. Use cookies to handle
Use a cookie to record the status of the form submission, depending on its status to check whether the form has been submitted, see the following code:
<?php
if (isset ($_post[' Go ')) {
Setcookie ("Tempcookie", "" ", Time () +30);
Header ("Location:". $_server[php_self]);
Exit ();
}
if (Isset ($_cookie["Tempcookie"])) {
Setcookie ("Tempcookie", "" ", 0);
echo "You have submitted a form";
}
?>
If the client prohibits cookies, this method will have no effect whatsoever, please note. For a detailed description of cookies, see Chapter 10th "PHP Session management."
PS: These several ways to prevent the recurrence of a form can be used as a reference, using the first way to prevent the form from being submitted multiple times.
Source Help guest Home Collection collation, reprint please specify the Source: www.bkjia.com/jingyan/