Repeated submission is a problem that we often encounter in our development, except that we use js to prevent the duplication of forms Submit, and you can also use php to prevent repeated submissions.
<?php /* * php中如何防止表单的重复提交 */ session_start(); if (empty($_SESSION['ip'])) {//第一次写入操作,判断是否记录了IP地址,以此知道是否要写入数据库 $_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; //第一次写入,为后面刷新或后退的判断做个铺垫 //...........//写入数据库操作 } else {//已经有第一次写入后的操作,也就不再写入数据库 echo '请不要再次刷新和后退'; //写一些已经写入的提示或其它东西 } ?>
Specific principles
Session scope variable token to prevent.
1. Open session:
session_start();
2. If a form is submitted
if (isset($token))
Token is included in the form in hidden form.
<input type="hidden" name="token" value="<?php echo $token; ?>" />
3. If the form is submitted repeatedly
if ($_SESSION["token"] != $token) { // 不让重复提交,在此处理 // header("location:".$_SERVER['PHP_SELF']); } else { // 正常的表单提交,在此处理 // echo "已提交"; }
4. Set token value
$token = mt_rand(0,1000000);
2$_SESSION['token'] = $token;
The above is about how to solve the problem of repeated submission of PHP forms. I hope it will be helpful to everyone's learning.