Home > php教程 > php手册 > [PHP]防止表单重复提交的几种方法

[PHP]防止表单重复提交的几种方法

WBOY
Release: 2016-06-06 19:49:40
Original
1056 people have browsed it

--------------------------------------------------------------------------------------------------- 1. 使用JS让按钮在点击一次后禁用(disable)。= 防止多次点击发生,实现方式较简单。 缺点:若客户端禁止JavaScript脚本,则失效。 2. 在提交成功后

---------------------------------------------------------------------------------------------------

1. 使用JS让按钮在点击一次后禁用(disable)。=> 防止多次点击发生,实现方式较简单。

  缺点:若客户端禁止JavaScript脚本,则失效。

 

2. 在提交成功后执行页面重定向(redirect)。=> 转到提交成功信息页面。

  特点:避免F5重复提交,消除浏览器前进和后退按导致的同样问题。

 

3. 表单隐藏域中存放session(表单被请求时生成的标记)。=> 接收表单数据后,检查此标志值是否存在,先进行删除,然后处理数据; 若不存在,说明已提交过,忽略本次提交。

<span>/*</span><span>
|---------------------------------
|原理 & 代码示例
|@黑眼诗人 <www.farwish.com>
|---------------------------------
</www.farwish.com></span><span>*/</span>
<span>//</span><span>服务端生成随机数存入session, 分配至表单页</span>
<span>$data</span>['sess_id'] = <span>$_SESSION</span>['sid'] = <span>mt_rand</span>(1000, 9999<span>);
</span><span>$this</span>->load->view('form', <span>$data</span><span>);

</span><span>//</span><span>表单页隐藏域存放此session值</span>
<input type="hidden" name="sid" value="<?=<span>$sess_id</span>; ?>">

<span>//</span><span>处理</span>
<span>if</span>(<span>$_POST</span>['sid'] != '' && $_POST['sid'] == $_SESSION['sid']<span>)
{
 </span><span>unset</span>(<span>$_SESSION</span>['sid'<span>]);

 </span><span>echo</span> '处理数据'<span>;
}
</span><span>else</span><span>
{
 </span><span>echo</span> '已提交过表单'<span>;
}</span>
Copy after login

 

4. 数据库唯一索引约束(最有效的防止重复数据的方法)。

 

Link: http://www.cnblogs.com/farwish/p/3911588.html

@黑眼诗人 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template