关于防止表单重复提交的做法有很多,本人知道的有如下,只是简单讲讲,大家觉得什么办法最简单又有效?
1.有redis的环境
把提交的动作action,参数之类的作为redis的key,存进redis,设置一个很短的缓存时间,提交的时候判断如果存在这个key,说明是重复提交数据。
2.jquery前端判断
除了在程序端判断重复提交外,在页面前端也加一层措施,如点击提交按钮,按钮状态不可用(有的按钮用的a标签),如果不是按钮button,可以用jquery在提交的时候设置一个属性值,如果有了这个属性值,表示已经提交。
$("body").data("applycancel","unlock");
3.表单隐藏域token
生成一个随机数,放进session,给表单加一个隐藏域放进token,提交的时候判断表单的token隐藏域和session的值是否一致,如果不一致表示重复提交,处理表单的时候unset掉这个session。
各位还有什么办法,什么办法最好,谢谢!
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把帖子内容改成了post-aa
。会产生的一个问题是,用户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.