How to prevent repeated forms from being submitted during page refreshing?

Source: Internet
Author: User
How to prevent repeated forms from being submitted during page refreshing! How to prevent repeated forms from being submitted during page refreshing! Hope you have code, comments, and ideas. thank you !, Use Cookie processing to record the status of form submission using Cookie. you can check whether the form has been submitted based on its status. See the following code: 1. & lt ;? How does php2. I prevent repeated forms from being submitted when pages are refreshed!
How to prevent repeated forms from being submitted during page refreshing!
Hope you have code, comments, and ideas. thank you!


------ Solution --------------------
Use Cookie processing to record the status of form submission using Cookie. you can check whether the form has been submitted based on its status. See the following code:

1. <? Php
2. if (isset ($ _ POST ['go']) {setcookie ("tempcookie", "", time () + 30 );
3. header ("Location:". $ _ SERVER [PHP_SELF]); exit ();
4.} if (isset ($ _ COOKIE ["tempcookie"]) {
5. setcookie ("tempcookie", "", 0); echo "form submitted ";
6.}?>
If the client disables the Cookie, this method does not have any effect. Please note that.

3. using Session processing with the PHP Session function can also prevent repeated submission of PHP forms. The Session is stored on the server. during PHP running, you can change the Session variable. The next time you access this variable, you get a new value. therefore, you can use a Session variable to record the value submitted in the form. if the value does not match, it is considered that the user is submitting it repeatedly. See the following code:

1. <? Php
2. session_start (); // Generate a random number based on the current SESSION
3. $ code = mt_rand (usd00 );
4. $ _ SESSION ['code'] = $ code;
5.?>
The random number is passed as a hidden value on the page form. the code is as follows:

1. <input type = "hidden" name = "originator" value = "<? = $ Code?> ">
The PHP code on the receiving page is as follows:

1. <? Php
2. session_start ();
3. if (isset ($ _ POST ['originator']) {
4. if ($ _ POST ['originator'] = $ _ SESSION ['code']) {
5. // The statement for processing the form, omitted
6.} else {
7. echo 'Please do not refresh this page or submit the form again! ';
8 .}
9.}?>

There are two ways not posted, please: http://www.phpnewer.com/index.php/Cjwt/detail/id/356

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.