Javascript prevents repeated form submission implementation code

Source: Internet
Author: User

Token-based processing is a permanent method. Copy codeThe Code is as follows: <script Language = 'javascript '>
Function formsubmit (){
Today = new Date ();
Var NowHour = Today. getHours ();
Var NowMinute = Today. getMinutes ();
Var NowSecond = Today. getSeconds ();
Var mysec = (NowHour * 3600) + (NowMinute * 60) + NowSecond;
If (mysec-document.formsubmitf.mypretime.value)> 600 ){
// 600 is only a time value, meaning repeated submission is prohibited within 5 minutes.
Document. formsubmitf. mypretime. value = mysec;
}
Else {
Alert ('one click is enough. Please do not submit it again! Please be patient! Thank you for your cooperation! ');
Return false;
}
Document. forms. formsubmitf. submit ();
}
</Script>

Prevent web page bounce-Disable caching
By controlling the browser cache and the page validity period, when you click the browser back button on the new page after redirection, a prompt "the page has expired" will be displayed to prevent the form from being submitted when the browser returns. However, this method is not effective for all browsers, so you should consider it before using it. In response, add the following code to the header to control the browser cache and the validity period of the webpage, or directly Add the following code to <meta> the webpage:Copy codeThe Code is as follows: <? Php
//.....
/// In IE 4 or 5, the Cache-Control mark is ignored and does not work.
Header ('cache-Control: no-cache ');
Header ('expires: 0 ');
// Valid for https. It is the same as "Expires:-1". In this case, the browser still caches the page, but marks the page as expired immediately.
Header ('pragma: no-cache ');
//....
?>

Action has such a method to generate a tokenCopy codeThe Code is as follows: protected String generateToken (HttpServletRequest request ){
HttpSession session = request. getSession ();
Try {
Byte id [] = session. getId (). getBytes ();
Byte now [] =
New Long (System. currentTimeMillis (). toString (). getBytes ();
MessageDigest md = MessageDigest. getInstance ("MD5 ");
Md. update (id );
Md. update (now );
Return (toHex (md. digest ()));
} Catch (IllegalStateException e ){
Return (null );
} Catch (NoSuchAlgorithmException e ){
Return (null );
}
}

Asp for instance
Form file formtest. aspCopy codeThe Code is as follows: <%
Randomize 'initial random number Seed
Num1 = rnd () 'generates a random number num1
Num1 = int (26 * num1) + 65' modify the num1 range to make it an Ascii code for the A-Z range to prevent form name errors
Session ("antry") = "test" & chr (num1) 'generates a random string
%>
<Form name = "test" action = "testact. asp" method = "post">
Your name: <input type = 'text' name = ''size = 30> 'note that the random form item name is used in this row.
<Input type = 'submit 'value = 'Submit'>
</Form>

Form handler testact. aspCopy codeThe Code is as follows: <%
Teststr = request. form (session ("antry "))
If teststr = "" then
Response. write "no name or repeated submission"
'The user did not enter the name, or the form was submitted repeatedly (marked as session ("antry") is empty ).
Else
Response. write teststr
Session ("antry") = "" 'submitted successfully. Clear session ("antry") to prevent repeated submission !!
End if
%>

Here, you only need to randomize a required project form item name, and do not need to randomize all form items

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.