PHP page Anti-Repetition submission method Summary _php Tutorial

Source: Internet
Author: User
1, submit button disabled

When the user submits, the button is immediately set to a unavailable state. This uses JS to achieve.

Before submission
Copy the Code code as follows:
$ ("#submit"). attr (' disabled ', ' true ');
$ ("#submit"). Val ("Being submitted, please wait a moment");

....................................................................................

After execution, set the button to its original state.

Copy the Code code as follows:
$ (' #submit '). Removeattr (' disabled ');
$ ("#submit"). Val ("OK to submit");


2. Expiration Time method

Idea: When the user submits the button into a token (each time a business submission token is a unique value), the session is stored and the expiration time is set. When the user submits the submission again, the detection token is consistent and expires, and if it is consistent and does not expire, it is considered to have been submitted two times. When the program executes an error, it needs to clear the value stored in session. See procedure below

Copy the Code code as follows:
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 waits for the entire page to be loaded, and the function can write immediately
Session_write_close ();
return true;
}
}

Delete the stored value

function Cancelrepeatsubmit () {

unset ($_session[' token ');
unset ($_session[' expire_time ');
}


3. Token Destruction Method

Idea: Generate tokens when the page is added, there is a session, and it is written in the form. Form submitted with the form submitted to the service side, the server through the session deposited token and token comparison, if equal, the destruction of tokens deposited in the seesion, when the page was two times submitted, due to deposit in the session token does not exist and error. Here is the code
Copy the Code code as follows:
/**
* Second option
* 1, generate tokens, and exist in the session
* 2, generated with the page
* 3, submit the page and the session to compare, after the success of the session to destroy
* 4, the second commit does not exist this value and error
* @param type $uniqueid
* @return Type
*/
function Createtoken ($uniqueid) {

$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;
}
}

The above summarizes three methods, the individual feels that the first kind with the second method with the use will achieve better results. The second method and the third method of personal feeling the third is to have the advantage point.

The second and the third method is to write tokens in the session, the advantage of this method is to save storage space, but the disadvantage is because the session is required to load the entire page to write, so when the entire page load is slow, and the user click multiple submissions, The system may also be considered the first input because the session has not been written. Causes validation to not work. Fortunately, the PHP function provides a good function. Session_write_close (), you can immediately write the session, without waiting for the page to load complete. Colleagues for the session of the deposit also have a number of ways to choose, can exist Redis,memcache or database can be.

http://www.bkjia.com/PHPjc/825207.html www.bkjia.com true http://www.bkjia.com/PHPjc/825207.html techarticle 1, submit button disabled when the user submits, immediately put the button is not available. This uses JS to achieve. Before committing, copy the code code as follows: $ ("#submit"). attr (' disabled ', ' t ...

  • 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.