Does page reload ever cause post?

跟風遠走 提交于 2019-11-26 14:47:29

Yes. If the page was loaded using POST data this will occur. To prevent this you need to implement the POST/REDIRECT/GET pattern.

Post/Redirect/Get (PRG) is a web development design pattern that prevents some duplicate form submissions, creating a more intuitive interface for user agents (users). PRG supports bookmarks and the refresh button in a predictable way that does not create duplicate form submissions.

Yes. If someone refreshes the browser manually, it will ask them if they want to send the form data again. This will cause that code to get executed.

A way to handle this is using tokens.

  1. Send a random string along with the post data
  2. Store this random string somewhere
  3. When you check the post data make sure that the stored string is the same as the string in the post request.
  4. If true, handle the request.
  5. Generate a new token

If someone were to refresh and resend the post data, your token in the post request will be different from the one you stored separately since you generated a new token at step 5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!