PHP prevent form Repeat submission Method _php Tutorial

Source: Internet
Author: User

PHP prevents forms from repeating the submission method


It is a tricky problem for users to submit a form because of the speed of the network, or if the webpage is maliciously flushed, causing the same record to be repeatedly inserted into the database. We can start with the client and the server side, and try to avoid repeating submissions for the same form.

1. Using Client Script

[Code]XM L Code:

1

When the user clicks the Submit button, the button changes to a gray unavailable state

The above example uses the On Click event to detect the user's submission status, and if the Submit button is clicked, the button is immediately invalidated and the user cannot click the button to submit again.

There is also a way to take advantage of the JA vasc ript function, but using the on Submit () method, if the form has been submitted once, will immediately pop up the dialog box, the code is as follows:

[Code]XM L Code:

01

submitcount=0 var;

submitonce function (form) {

if (Submitcount = = 0) {

submitcount++;

return true;

else{}

Alert ("is operating, please do not repeat the submission, thank you!");

return false;

10}

11}

12

13

In the example above, if the user has clicked the Submit button, the script automatically records the current state and adds the Submitcount variable to 1, and when the user tries to commit again, the script determines that the value of the Submitcount variable is nonzero, prompting the user to submit it, thereby avoiding repeating the form.

2. Use the session (this is the same as the JSP processing method)

You can also avoid repeating the form by using the session function of PHP. Session is saved on the server side, in the PHP run process can change the session variable, the next time you access this variable, get a new assignment value, so, you can use a session variable to record the value of the form submission, if not match, it is considered that the user is repeating the commit.

The code for the A page:

[Code]php Code:

01

Session_Start (); Generate random numbers based on current session

$code = Mt_rand (0,1000000);

$_session[' Code ' = $code; Temporarily deposit this random number to session

?>

06

b page:

[Code]php Code:

01

02session_start ();

03if (Isset ($_post[' originator ')) {

if ($_post[' originator '] = = $_session[' code ') {

echo "OK";

unset ($_session["Code"]); Erase it now and press F5 to void it.

}else{

echo "Please do not refresh this page or repeat the submission form";

09}

10}?>

http://www.bkjia.com/PHPjc/1043516.html www.bkjia.com true http://www.bkjia.com/PHPjc/1043516.html techarticle php Prevent form Repeat submission method user submits the form may because of the network speed reason, or the webpage is maliciously refreshes, causes the same record to insert repeatedly into the database, this is a ratio ...

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