關(guān)于防止表單重復(fù)提交的做法有很多,本人知道的有如下,只是簡單講講,大家覺得什么辦法最簡單又有效?
1.有redis的環(huán)境
把提交的動作action,參數(shù)之類的作為redis的key,存進(jìn)redis,設(shè)置一個很短的緩存時間,提交的時候判斷如果存在這個key,說明是重復(fù)提交數(shù)據(jù)。
2.jquery前端判斷
除了在程序端判斷重復(fù)提交外,在頁面前端也加一層措施,如點(diǎn)擊提交按鈕,按鈕狀態(tài)不可用(有的按鈕用的a標(biāo)簽),如果不是按鈕button,可以用jquery在提交的時候設(shè)置一個屬性值,如果有了這個屬性值,表示已經(jīng)提交。
$("body").data("applycancel","unlock");
3.表單隱藏域token
生成一個隨機(jī)數(shù),放進(jìn)session,給表單加一個隱藏域放進(jìn)token,提交的時候判斷表單的token隱藏域和session的值是否一致,如果不一致表示重復(fù)提交,處理表單的時候unset掉這個session。
各位還有什么辦法,什么辦法最好,謝謝!
認(rèn)證高級PHP講師
Use Etag to implement optimistic locking mechanism in concurrency control. The following example is not limited to repeated submissions:
In a concurrent scenario, when multiple clients operate the same resource at the same time, a situation will arise: the resource operated by the client is changed under unknown circumstances.
A simple example:
User A posted a post with the content post-a
,用戶B看到了post-a
之后在下面寫評論,在B寫評論期間用戶A把帖子內(nèi)容改成了post-aa
。會產(chǎn)生的一個問題是,用戶B針對post-a
The comment he wrote looks weird^_^
The solution is very simple. When B sees A, give him a post resource identification tag-1. When B submits a comment, he submits it together with this identification tag-1. The server verifies the a priori conditions before accepting the request. If there is no change in the post content at that time, it is still tag-1, which means success.
If the content of the post changes, the new identification is tag-2, which does not match the tag-1 submitted by B, indicating that the resources have changed during the period, and 412 Precondition Failed will be returned. Then refresh the page or prompt, let B know this change and continue to comment.
The API situation is similar, you just need to add an identifier to the returned resources.
Generate a hash value from the parameters passed in, and store it in the hash_code field of the database. This field is used as a unique index. And each time it is inserted into the database, a hash value is generated in the same way to determine whether the hash value exists.