你是否遇到過“重復提交”的問題?要解決這個問題其實并不難。這里有一個簡單的方法避免同一表單的重復提交。
首先,我們可以定義一個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的灌水辦法。究竟有沒有用,還是看各位站長的喜好了。
新聞熱點
疑難解答