直播中
原文:
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
}
譯自:phpbuilder
這只是一個小技巧,用來避免一個表單的重復(fù)提交。這樣多少可以防止一些灌水的現(xiàn)象,另外有時候由于網(wǎng)絡(luò)狀況等原因用戶不知道提交是否成功,也會再次提交同一份表單。
這個技巧的主要原理是不允許用戶回退后再次提交,也就是說回退后修改再提交也是不允許的,而且也不能避免Ctrl-C/Ctrl-V的灌水辦法。究竟有沒有用,還是看各位站長的喜好了。
【本文版權(quán)歸原作者guestzzz及奧索網(wǎng)共同擁有。如需轉(zhuǎn)載,請注明原作者及出處】