PHP common methods to prevent repeated submission of forms

Source: Internet
Author: User
This article mainly introduces several common methods for PHP to prevent repeated submission of forms, which has good practical value in PHP project development, for more information, see the following section to describe several common methods for PHP to prevent repeated submission of forms. this is of great practical value in PHP program development. The specific method is as follows:

1. use JS to make the button disabled after one click (disable ). This method can prevent multiple clicks and is easy to implement.

The disadvantage is that JavaScript scripts are disabled on the client.

2. after the submission is successful, execute page redirection (redirect ). Go to the submitted information page.

Features: avoid repeated submission of F5 and eliminate the same problems caused by buttons forward and backward of the browser.

3. store the session in the form hiding field (the Mark generated when the form is requested ). After receiving form data, use this method to check whether the flag value exists. first, delete the value and process the data. if the value does not exist, it indicates that it has been submitted. ignore this submission.

/* // The server generates a random number and stores it in the session. the random number is allocated to the table list page $ data ['sess _ id'] = $ _ SESSION ['Sid '] = mt_rand (1000,999 9 ); $ this-> load-> view ('form', $ data); // hide the field on a single table page to store this session value// Process if ($ _ POST ['Sid ']! = ''& $ _ POST ['Sid '] = $ _ SESSION ['Sid']) {unset ($ _ SESSION ['Sid ']); echo 'process data';} else {echo 'submitted form ';}

4. unique database index constraints (the most effective way to prevent duplicate data ).

I hope the methods described in this article can help you develop PHP projects.

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.