Php solution form repeated submission implementation method introduction _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php solves the problem of repeated submission of forms. Php solves the problem of repeated submission of forms. Repeated submission is a common problem during development. in addition to using js to prevent repeated submission of forms, you can also use php to solve the problem of repeated form submission.

Repeated submission is a common problem during development. in addition to using js to prevent repeated submission of forms, php can also be used to prevent repeated submission.

Example 1

The code is as follows:

  

/*

* How to prevent repeated forms from being submitted in php

*/

Session_start ();

If (empty ($ _ SESSION ['IP']) {// The first write operation to determine whether the ip address is recorded and whether to write data to the database

$ _ SESSION ['IP'] = $ _ SERVER ['remote _ ADDR ']; // writes data for the first time, paving the way for refresh or rollback judgment.

// .......... // Write data to the database

} Else {// the operation after the first write will no longer be written to the database

Echo 'Please do not refresh and rewind '; // write some written prompts or other things

}

?>

Principle

The token of the session range variable is prevented.

1. enable session:

Session_start ();

2. if a form is submitted

The code is as follows:

If (isset ($ token ))

The token is included in the form of hidden.

The code is as follows:

  

3. if the form is submitted repeatedly

The code is as follows:

1. if ($ _ SESSION ["token"]! = $ Token ){

2. // do not allow repeated submission, which will be processed here

3. // header ("location:". $ _ SERVER ['php _ SELF ']);

4.} else {

5. // normal form submission, which is processed here

6. // echo "submitted ";

7 .}

4. set the token value

The code is as follows:

1. $ token = mt_rand (0 );

2. $ _ SESSION ['token'] = $ token;

Implementation of php solution form repeated submission

------ Solution --------------------

Is the second normal submission allowed?

------ Solution --------------------

Actually, this is not the case.

Because mature projects generally separate views and forward them through routes! This will not happen!

Http://www.bkjia.com/PHPjc/820420.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/820420.htmlTechArticlephp to solve the form repeated submission implementation method introduction repeated submission is a problem we often encounter in the development, in addition to we use js to prevent the form of repeated submission, but also can use...

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.