首页 > 开发 > PHP > 正文

PHP小技巧:避免表单的重复提交的方法

2024-05-04 23:03:16
字体:
来源:转载
供稿:网友

你是否遇到过“重复提交”的问题?要解决这个问题其实并不难。这里有一个简单的方法避免同一表单的重复提交。

首先,我们可以定义一个session变量用来保存一个表单的提交序列号。这里我定义为“$userlastaction”。

然后在表单里加入一个hidden变量,把值设为$userlastaction+1:

<input type=hidden name=lastaction value=<? =$userlastaction+1 ?>>

最后,在处理提交之前判断表单是否已被提交过:

if($lastaction>$userlastaction and inputisvalid(...)){
$userlastaction++; // 序列号加1
// 处理表单数据
}

avoid multiple form submissions
submitted by: douglas e. cook
date: 07/26/00 19:46
does your database suffer from "duplicate post" syndrome? the cure isn't too difficult. here is a simple way to prevent users from submitting the same form multiple times.

first, declare a session variable to store a serial number for each form. i call mine "$userlastaction." then, in every form where duplicate submission is a problem, include a hidden field, and set the value to $userlastaction+1:

<input type=hidden name=lastaction value=<?= $userlastaction+1 ?>>

finally, verify that the form has not been previously submitted before acting on the submission:

if($lastaction>$userlastaction and inputisvalid(...)){
$userlastaction++; // increment serial number
// act on form here
}

这只是一个小技巧,用来避免一个表单的重复提交。这样多少可以防止一些灌水的现象,另外有时候由于网络状况等原因用户不知道提交是否成功,也会再次提交同一份表单。

这个技巧的主要原理是不允许用户回退后再次提交,也就是说回退后修改再提交也是不允许的,而且也不能避免ctrl-c/ctrl-v的灌水办法。究竟有没有用,还是看各位站长的喜好了。

  • 网站运营seo文章大全
  • 提供全面的站长运营经验及seo技术!
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表