Several common ways to prevent form duplication

One: Form repeated submission is the most common problem in multi-user web applications and brings a lot of trouble. Repeated submission problems such as:

Click the submit button twice.
Click the refresh button.
Repeating the previous action with the browser back button resulted in repeated submission of the form.
Repeat form submission using browser history.
Browser repeated HTTP requests.

Two: the way

1. js disables the submit button.

Use Javascript to disable the submit button after the form is submitted. This approach prevents impatient users from clicking the button multiple times. But there is a problem, if the client side disables Javascript, this method will not work.

2. Store a special flag in the session.

On the server side, generate a unique identifier, store it in the session, and write it into the hidden field of the form, and then send the form page to the browser. After the user enters the information, click submit, and on the server side, get the form The value of the hidden field in the session is compared with the unique identifier in the session. If it is equal, it means that it is submitted for the first time, so the request will be processed, and then the unique identifier in the session will be removed.

3. Use Post/Redirect/Get mode.

Perform page redirection after submission, this is the so-called Post-Redirect-Get (PRG) pattern. In short, when the user submits the form, you perform a client-side redirection to the submission success message page.

This avoids the double submission caused by the user pressing F5, and it also does not have the browser form double submission warning, and also eliminates the same problem caused by pressing forward and backward in the browser.

4. Processing of form expiration

In the development process, it often happens that the form is wrong and all the information filled in when returning to the page is lost. In order to support the page bounce, the following two methods can be used.

1). Use the header header to set the Cache-control header.

header('Cache-control: private, must-revalidate'); //Support page jump back

2). Use the session_cache_limiter method.

session_cache_limiter('private, must-revalidate'); //Write before session_start method

The following code snippet prevents the user from filling out the form, and clicking the "Submit" button to return, the content just filled in on the form will not be cleared:

session_cache_limiter('nocache');

session_cache_limiter('private');

session_cache_limiter('public');

session_start();

//The following is the content of the form, so that when the user returns to the form, the content that has been filled in will not be cleared

Paste this code at the top of the script you want to apply.

5. Add constraints in the database.

Add unique constraints or create unique indexes in the database to prevent duplicate data. This is the most effective way to prevent double submission of data.

How did you overcome the data duplication problem? Have you come across any real-world examples of duplicating data submissions?

Reprinted from: http://www.bkjia.com/jingyan/471187.html

6. Use client-side scripting

When it comes to client-side scripting, JavaScript is often used for general input validation. In the following example, we use it to deal with the repeated submission of the form, please see the code below:

<form method="post" name="register" action="test.php" enctype="multipart/form-data">

<input name="text" type="text" id="text" />

<input name="cont" value="提交" type="button" onClick="document.register.cont.value='正在提交,请等待...';document.register.cont.disabled=true;document.the_form.submit();">

</form>

When the user clicks the "Submit" button, the button will be grayed out and unavailable.

In the above example, the OnClick event is used to detect the user's submission status. If the "Submit" button is clicked, the button is immediately set to an invalid state, and the user cannot click the button to submit again.

7. Handling with cookies

Use cookies to record the status of form submission, according to its status, you can 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 "您已经提交过表单";

}

?>

Note that this method will have no effect if cookies are disabled by the client. For a detailed introduction to cookies, see Chapter 10, "PHP Session Management"

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324382665&siteId=291194637