Explore the prevention rules of PHP form repeated submission

Source: Internet
Author: User

After you click the submit button, the button becomes unavailable.

In the above example, The OnClick event is used to detect the user's submission status. If you click the submit button, the button is immediately set to invalid. You cannot click the button to submit again.

Another method is to use JavaScript, but the OnSubmit () method is used. If a form has been submitted once, the dialog box is displayed immediately. The Code is as follows:

 
  1. <Input type = "hidden" name = "originator" value = "<? = $ Code?> ">
  2. The PHP code on the receiving page is as follows:
  3. <? Phpsession_start (); if (isset ($ _ POST ['originator']) {
  4. If ($ _ POST ['originator'] = $ _ SESSION ['code']) {// statement for processing the form, omitted
  5. } Else {
  6. Echo 'Please do not refresh this page or submit the form again! ';
  7. }
  8. }?>


In the preceding example, if you have clicked the submit button, the script automatically records the current status and automatically adds the submitcount variable
1. When the user tries to submit the submitcount variable again, the script determines that the value of the submitcount variable is non-zero and prompts the user to submit the table again to avoid repeated submission.

2. Use cookies for processing
Use cookies to record the status of the Form submission. 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 "you have submitted a form ";
  6. }?>

If the client disables the Cookie, this method does not have any effect. Please note that.

3. Use Session Processing
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. }?>

4. Use the header function to redirect
In addition to the preceding PHP form repeated submission prevention method, there is also a simpler method, that is, when the user submits the form, the server immediately redirects to other pages after processing, the Code is as follows.

 
  1. If (isset ($ _ POST ['action'])
    & $ _ POST ['action'] = 'submitted '){
  2. // Process the data. For example, after inserting the data, immediately go to another page.
  3. Header ('location: submits_success.php ');
  4. }

The preceding section describes several preventive methods for repeated submission of PHP forms.


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.