python - 如何防止狂点提交表单按钮导致的数据重复?
高洛峰
高洛峰 2017-04-18 10:05:45
0
12
1675
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(12)
洪涛

Disable the button immediately after clicking it. If the submission fails, restore the button, otherwise it will be redirected to the success page.

Or after clicking, a mask will pop up.

However, both of the above solutions require js. Why don't you want to use js?

迷茫

1.JS button prevents repeated clicks

2. Server cache control, the method is to write the library repeatedly

3. Create a unique index in the database (for example, the post title is unique and cannot be repeated)

阿神

Can be controlled from the front and backend:

  • Add front-end control logic, when the user clicks to post, disable the "Post" button

  • Add background control logic. When a post request is sent, check whether the person has sent a post with the same title and content. If so, prohibit the creation of this duplicate post

迷茫

You can generate an id when rendering the template, then submit the id together when submitting, and then compare it with the backend, so that if the passed id is incorrect, then the processing will be ignored.

PHPzhong

First, a simpler solution is to use the debounce function to limit the frequency of DOM callback triggers.

Second, if you still have to consider the situation where users disable js in this era, then don’t do it on the front end.

洪涛

This problem cannot be solved by relying on the front end alone.

  1. Add caching, use map for single machine, and use redis for distributed caching.

  2. Use Mysql’s stored procedure to hand over the concurrency pressure to the database

  3. Add lock, optimistic lock or pessimistic lock

  4. Unique key

刘奇

The current page generates a unique id. When submitting, the server determines that it will only be processed once, such as laravel的表单提交页都有个csrf_token

刘奇

Then write one yourself in the backgroundcsrfOkay, let me give you some ideas, automatically generate a key, and bring the key when submitting the content. If they are the same, reset the key or clear it. If they are different, it is submitted again.

迷茫

Form form post past a random string from a hidden field and put it into the session
The program receives this random string and compares it with the string in the session. If it is the same, it will be submitted. If it is not the same, it will prompt an error. It needs to be reset when comparing. This session is enough

Ty80

1.JS verification, disable the submit button after submission
2. Server-side verification, every time the page is refreshed, a token will be generated in the page. When submitting data to the server, it will be judged whether the token is valid, then the data will be processed and the token will be destroyed. If the front-end repeats The submission will definitely not be processed because the token has been destroyed

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template