Summary of methods to prevent repeated data submission

Source: Internet
Author: User

This article summarizes a large number of methods to solve repeated data submission by users, such as php, js, asp.net, and java. For more information, see this article.

Php cookie Processing Method

If the data passes the preceding two verifications, the data is valid and valid. At this time, we concatenate the submitted data into a string and use MD5 encryption to obtain an MD5 value. Then we put this value on the client through cookies, when the user submits the form next time, we repeat this step and read the MD5 value in the Cookie for comparison, if they are the same, we can conclude that the two submitted forms are the same. Otherwise, this Cookie will be replaced!

The Code is as follows: Copy code

<? Php
$ Value = $ _ COOKIE ["value"]; // read the last set Cookie value
If (count ($ _ POST )){
$ Long = "";
While (list ($ key, $ value) = each ($ _ POST) $ long. = $ value;
$ Hash = md5 ($ long );
Setcookie ("value", $ hash, time () + 60*60); // reset the cookie
}
If ($ value! = $ Hash ){
// Perform further operations on the data if the MD5 values are different twice
} Else {
// If the two MD5 values are the same, the user is notified that the submission failed and the form is submitted repeatedly.
}
?>

Js Solution

1. Refresh multiple times during submission, causing the script on the server to accept data multiple times

Solution

Step 1: use javascript Functions to trigger the submit Of the form in the front-end form. Which of the following statements do not validate the input content? How terrible is insert?

Example:

The Code is as follows: Copy code

<Form id = "signupform">
<Input type = "text" name = "username"/>
<Input type = "submit" id = "signbtn" value = "next"/>
</Form>
<Script type = "text/javascript">
$ ('# Signbtn'). click (function (){
$ (This). attr ('Disabled ', 'true ');
// Form validate process
$ ('# Signupform'). submit ();
Return false;
});
</Script>


Use the browser cache to roll back and then submit

Step 1: Clear the previous address record by force after processing. like location in javascript. replace or a Location like in PHP, even if this step is not used, it doesn't matter. we only need to clear the values saved by the browser.
Step 2: Add the following JS functions to the form page

The Code is as follows: Copy code


$ (Window). load (function (){
$ ('# Signupform'). find ('input, textarea '). not (': submit,: hidden,: rest'). val ('');
});

You can also use Form reset () of javascript DOM as follows:

The Code is as follows: Copy code

$ ('# Signupform'). each (function (){
This. reset ();
})


Asp.net Solution

The solution is to store the Session ID and the SessionID stored in ViewState at the time of submission to Prevent Users From refreshing the screen. If Automatic sending back is allowed in your program, if not, you have to store this variable in the hidden field. The following is a typical example. In the Page_Load event, you store the SessionID and timestamp of the first commit.

The Code is as follows: Copy code

Protected System. Web. UI. WebControls. Button SubmitButton;

Protected System. Web. UI. WebControls. Label RefreshID;

Private void Page_Load (object sender, System. EventArgs e)

{
If (RefreshID. Text. Length = 0)
{
RefreshID. Text = Session. SessionID + DateTime. Now. Ticks. ToString ();
}
}
 

Private void button#click (object sender, System. EventArgs e)

{
String sesToken = (string) Session [FrameworkConst. SYNC_CONTROL_KEYWORD]; string pageToken = RefreshID. Text;
If (sesToken! = Null & sesToken! = PageToken)
{
Response. Write ("The Refresh was saved med after submit .");
}
Else
{
// Do your processing here to avoid Refresh trap
Response. Write ("The processing is done here. Disabling submit
Button so that user can not perform multiple submit .");
Response. Write ("But still user can peform Refresh on page .");
}

Session [FrameworkConst. SYNC_CONTROL_KEYWORD] = Session. SessionID + DateTime. Now. Ticks. ToString ();
RefreshID. Text = sesToken;
SubmitButton. Enabled = false;

}

Repeated jsp submission

There are several ways to read the online:
1. Add this code to the HEAD area on your table list page:

The Code is as follows: Copy code
<META HTTP-EQUIV = "pragma" CONTENT = "no-cache">
<META HTTP-EQUIV = "Cache-Control" CONTENT = "no-cache, must-revalidate">
<META HTTP-EQUIV = "expires" CONTENT = "Wed, 26 Feb 1997 08:21:57 GMT">

2. Generate a token and save it in the user session. Add a hidden domain to the form to display the token.
The value of the card. After the form is submitted, a new token is generated, and the token and session submitted by the user are
Token comparison in. If the token is the same, it is submitted repeatedly.

3. Use the Response. Redirect ("selfPage") statement in your server control code. However, this method is not used in most cases.
There are many other methods...

4

The Code is as follows: Copy code
<Input type = "button" value = "submit" onclick = "this. disabled = true; this. form. submit ()">

5. Add a hidden field to the FORM on the JSP page.

The Code is as follows: Copy code
<Input type = "hidden" name = "url" value = <% = request. getRequestURL () %>


Add the following statement to your serverlet (Java code ):

The Code is as follows: Copy code
String url = request. getParameter ("url ");
Response. sendRedirect (url );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.