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

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

Jun 06, 2016 pm 07:49 PM
phpseveral kindssubmitmethodformrepeatprevent

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

 

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

 

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

@黑眼诗人 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment