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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft