Summary of anti-repeated page submission methods

Source: Internet
Author: User
Sometimes our program execution is slow, and our page is not very friendly and there is no prompt. The operator thinks that if the submit button is not clicked, the user will click submit again. This will cause many problems. The following describes three methods to prevent repeated submission. 1. when the submit button is set to disabled, sometimes our program execution is slow, and our page is not very friendly and there is no prompt. The operator thinks that if the submit button is not clicked, the user will click submit again. This will cause many problems. The following describes three methods to prevent repeated submission. 1. the submit button is set to disabled. after the user submits the button, the button is immediately set to unavailable. This is implemented using js. $ ("# Submit") before submission "). attr ('disabled ', 'true'); $ ("# submit "). val ("submitting, please wait ");................................... ........................................ ......... after execution, set the button to the original state $ ('# submit '). removeAttr ('disabled '); $ ("# submit "). val ("confirm to submit"); 2. idea of the Expiration Time Method: when the user submits a button, generate a token (each time the service submits a token as a unique value) and save it to the session, and set the Expiration Time. When you submit the token again, check whether the token is consistent and has expired. if the token is consistent and has not expired, the token is considered to have been submitted twice. When an error occurs in program execution, you need to clear the value stored in the session. See the following program function checkRepeatSubmit ($ uniqueid = '', $ expire = 30) {$ uniqueid = empty ($ uniqueid )? Yii: app ()-> user-> id. yii: app ()-> user-> name. yii: app ()-> user-> mihome: $ uniqueid; $ token = md5 ("wms_check_repeat ". $ uniqueid); $ time = time (); if (isset ($ _ SESSION ['token']) &! Empty ($ _ SESSION ['token']) & $ _ SESSION ['token'] ==$ token & ($ time-$ _ SESSION ['expire _ time'] <$ expire) {return false ;} else {$ _ SESSION ['token'] = $ token; $ _ SESSION ['expire _ time'] = $ time; // when the session is written, it will wait until the whole page is loaded. with this function, session_write_close (); return true ;}// delete the stored value function cancelRepeatSubmit () {unset ($ _ SESSION ['token']); unset ($ _ SESSION ['expire _ time']);} 3. Methods of token destruction: generated when the page is installed Token, which exists in the session and is written in the form. When a form is submitted, it is submitted to the server along with the form. the server compares the tokens stored in the session with the tokens. if the tokens are equal, the tokens saved in the seesion are destroyed. when the page is submitted twice, an error is returned because the token stored in the session does not exist. The following is the code/*** second solution ** 1. generate token and exist in session * 2. generate with page * 3. submit the page and compare it with the session, destroy session after successful submission * 4. if this value does not exist during the second submission, the error * @ param type $ uniqueid * @ return type */function createToken ($ uniqueid) is returned) {$ uniqueid = empty ($ uniqueid )? Yii: app ()-> user-> id. yii: app ()-> user-> name. yii: app ()-> user-> mihome: $ uniqueid; $ token = md5 ("wms_check2_repeat ". $ uniqueid); $ _ SESSION ['form _ token'] = $ token; session_write_close (); return $ token;} function checkToken ($ token) {if (! Isset ($ _ SESSION ['form _ token']) | empty ($ _ SESSION ['form _ token']) | $ _ SESSION ['form _ token']! = $ Token) {return false;} else {unset ($ _ SESSION ['form _ token']); return true ;}} three methods are summarized above, I personally feel that the first and second methods can work together to achieve better results. The second method and the third method are personal feelings. The third method must have advantages. Both the second and third methods write the token in the session. the advantage of this method is that it saves storage space, but the disadvantage is that the session can be written only after the whole page is loaded, therefore, when the whole page is slow to load and the user clicks to submit multiple times, the system may think it is the first input because the session has not been written. The verification fails. Fortunately, php functions provide a powerful function. Session_write_close (). you can write the session immediately without waiting for the page to load. There are also many ways for colleagues to store sessions, including redis, memcache, and database.

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.