PHP introduction to solving the problem of repeated form submission_PHP tutorial
WBOYOriginal
2016-07-20 11:12:10793browse
Repeated submission is a problem we often encounter in development. In addition to using js to prevent repeated submission of forms, we can also use php to prevent repeated submission.
/* * How to prevent repeated submission of forms in php
*/
session_start();
if ( empty($_SESSION['ip'])) {//The first write operation, determine whether the IP address is recorded, so as to know whether to write to the database
$_SESSION['ip'] = $_SERVER[ 'REMOTE_ADDR']; //First write, paving the way for subsequent refresh or retreat judgments
//.........//Writing database operation
} else {//There has been an operation after the first write, so it will no longer be written to the database
echo 'Please do not refresh and back again'; //Write some prompts or other things that have been written
if (isset($token))Token is included in the form in hidden form.
The code is as follows
Copy code
3. If the form is submitted repeatedly
The code is as follows
Copy code
1.if ($_SESSION["token"] != $token) { 2. // Do not allow repeated submissions. This processing3. // header("location:".$_SERVER['PHP_SELF']); 4.} else { 5. // Normal form submission, processed here6. // echo "Submitted"; 7.} 4. Set token value
The code is as follows
Copy code
1.$token = mt_rand (0,1000000); 2.$_SESSION['token'] = $token;
http://www.bkjia.com/PHPjc/444586.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444586.htmlTechArticleRepeated submission is a problem we often encounter in our development. In addition to using js to prevent repeated submission of forms, , and you can also use php to prevent repeated submissions. Example 1 The code is as follows...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn