如何避免表單的重復(fù)提交原理詳解
這個技巧的主要原理是不允許用戶回退后再次提交,也就是說回退后修改再提交也是不允許的,而且也不能避免Ctrl-C/Ctrl-V的灌水辦法。究竟有沒有用,還是看各位站長的喜好了
你是否遇到過“重復(fù)提交”的問題?要解決這個問題其實并不難。這里有一個簡單的方法避免同一表單的重復(fù)提交。
首先,我們可以定義一個session變量用來保存一個表單的提交序列號。這里我定義為“$userLastAction”。然后在表單里加入一個hidden變量,把值設(shè)為$userLastAction+1:
<input type=Hidden name=lastAction value=<? =$userLastAction+1 ?>>
最后,在處理提交之前判斷表單是否已被提交過:
if($lastAction>$userLastAction and inputIsValid(...)){
$userLastAction++; // 序列號加1
// 處理表單數(shù)據(jù)
}
原文:
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
}
推薦文章
2025-01-18
2024-11-28
2024-11-09
2024-10-25
2024-06-25
2024-01-04
2023-11-06
2023-10-30
2023-10-13
2023-10-10
穩(wěn)定
產(chǎn)品高可用性高并發(fā)貼心
項目群及時溝通專業(yè)
產(chǎn)品經(jīng)理1v1支持快速
MVP模式小步快跑承諾
我們選擇聲譽堅持
10年專注高端品質(zhì)開發(fā)聯(lián)系我們
友情鏈接: