查看完整版本: 避免表单的重复提交的方法

ivanisme 2008-2-26 09:28

避免表单的重复提交的方法

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

首先,我们可以定义一个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的灌水办法。究竟有没有用,还是看各位站长的喜好了。

beauty511 2008-3-19 23:59

beauty511

[URL=http://www.beauty511.com/shop2/index.php]wedding dress[/URL]
[URL=http://www.beauty511.com/shop2/catalog_31.html]bride[/URL]
[URL=http://www.beauty511.com/]abito[/URL]
[URL=http://www.beauty511.com/]vestido[/URL]
[URL=http://www.beauty511.com/]abito[/URL]
[URL=http://www.beauty511.com/Girls.php]flower gril[/URL]
[URL=http://www.beauty511.com/Voyage.php]party dress[/URL]
[URL=http://www.beauty511.com/]evening dress[/URL]
[url]http://www.beauty511.com[/url]
[URL=http://www.beauty511.com/]traje de novia[/URL]
[URL=http://www.beauty511.com/]Brautkleider[/URL]
<a href="http://www.beauty511.com/shop2/index.php" >wedding dress</a>
<a href="http://www.beauty511.com/shop2/catalog_31.html" >bride</a>
<a href="http://www.beauty511.com/" >abito</a>
<a href="http://www.beauty511.com/Girls.php" >flower gril</a>
<a href="http://www.beauty511.com/Voyage.php" >party dress</a>
<a href="http://www.beauty511.com/" >evening dress</a>
<a href="http://www.beauty511.com/" >vestido</a>
<a href="http://www.beauty511.com/" >traje de novia</a>
<a href="http://www.beauty511.com/" >Brautkleider</a>
[url]http://www.beauty511.com/[/url]
页: [1]
查看完整版本: 避免表单的重复提交的方法