PHP防止表单重复提交的几种常用方法汇总_PHP

WBOY
Release: 2016-05-31 19:30:33
Original
776 people have browsed it

本文较为详细的汇总了PHP防止表单重复提交的几种常用方法,在PHP程序开发中有着很高的实用价值。具体方法如下:

1. 使用JS让按钮在点击一次后禁用(disable)。采用这种方法可以防止多次点击的发生,实现方式较简单。

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

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

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

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

/*
//服务端生成随机数存入session, 分配至表单页
$data['sess_id'] = $_SESSION['sid'] = mt_rand(1000, 9999);
$this->load->view('form', $data);

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

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

 echo '处理数据';
}
else
{
 echo '已提交过表单';
}

Copy after login

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

希望本文所述方法对大家的PHP项目开发能起到一定的帮助作用。

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!