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