Home > Backend Development > PHP Tutorial > How to solve the problem of repeated submission of php forms, repeated submission of php forms_PHP tutorial

How to solve the problem of repeated submission of php forms, repeated submission of php forms_PHP tutorial

WBOY
Release: 2016-07-12 09:07:51
Original
910 people have browsed it

How to solve the problem of repeated submission of php forms, repeated submission of php forms

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.

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

Specific principles
Session scope variable token to prevent.
1. Open session:
session_start();
2. If a form is submitted

 if (isset($token))
Copy after login

Token is included in the form in hidden form.

<input type="hidden" name="token" value="<&#63;php echo $token; &#63;>" />
Copy after login

3. If the form is submitted repeatedly

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

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1058161.htmlTechArticleHow to solve the problem of repeated submission of php forms. Repeated submission of php forms is a common problem that we encounter in our development. Problem, except that we use js to prevent repeated submission of the form, and at the same time...
Related labels:
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