Home > Backend Development > PHP Tutorial > PHP introduction to solving the problem of repeated form submission_PHP tutorial

PHP introduction to solving the problem of repeated form submission_PHP tutorial

WBOY
Release: 2016-07-20 11:12:10
Original
820 people have browsed it

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.

Example 1

1. Start session:
The code is as follows
 代码如下 复制代码

/*
* php中如何防止表单的重复提交
*/
session_start();
if (empty($_SESSION['ip'])) {//第一次写入操作,判断是否记录了IP地址,以此知道是否要写入数据库
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; //第一次写入,为后面刷新或后退的判断做个铺垫
//...........//写入数据库操作
} else {//已经有第一次写入后的操作,也就不再写入数据库
echo '请不要再次刷新和后退'; //写一些已经写入的提示或其它东西
}
?>

Copy code


/*

* 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))

?>


 代码如下 复制代码


Specific principle
 代码如下 复制代码

1.if ($_SESSION["token"] != $token) {
2.    // 不让重复提交,在此处理
3.    // header("location:".$_SERVER['PHP_SELF']);
4.} else {
5.    // 正常的表单提交,在此处理
6.    // echo "已提交";   
7.}

session scope variable token to prevent.
 代码如下 复制代码

1.$token = mt_rand(0,1000000);
2.$_SESSION['token'] = $token;

session_start();
2. If a form is submitted

The code is as follows
Copy code
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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template