页面刷新时如何防止表单重新提交(F5/CTRL+R)

2022-01-30 00:00:00 post forms php html form-submit

我有一个向我的 SQL 表提交文本的简单表单.问题是用户提交文本后,他们可以刷新页面并再次提交数据,而无需再次填写表单.提交文本后,我可以将用户重定向到另一个页面,但我希望用户留在同一页面上.

I have a simple form that submits text to my SQL table. The problem is that after the user submits the text, they can refresh the page and the data gets submitted again without filling the form again. I could redirect the user to another page after the text is submitted, but I want users to stay on the same page.

我记得读过一些关于为每个用户提供唯一会话 ID 并将其与另一个值进行比较的内容,这解决了我遇到的问题,但我忘记了它在哪里.

I remember reading something about giving each user a unique session id and comparing it with another value which solved the problem I am having but I forgot where it is.

推荐答案

使用 Post/Redirect/Get 模式.http://en.wikipedia.org/wiki/Post/Redirect/Get

Use the Post/Redirect/Get pattern. http://en.wikipedia.org/wiki/Post/Redirect/Get

对于我的网站,我会将消息存储在 cookie 或会话中,在发布后重定向,读取 cookie/会话,然后清除该会话或 cookie 变量的值.

With my website, I will store a message in a cookie or session, redirect after the post, read the cookie/session, and then clear the value of that session or cookie variable.

相关文章