如何在 POST + HTTP 重定向后向用户显示消息 [英] How to display messages to the user after a POST + HTTP redirect

查看:28
本文介绍了如何在 POST + HTTP 重定向后向用户显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PRG 模式来避免多次提交表单.然而,它有一个严重的缺点——你不能简单地echo给用户确认消息(显然,用户不会看到这个页面,他会被重定向到另一个页面).

I’m using the PRG pattern to avoid multiple form submission. It has, however, a serious drawback — you cannot simply echo the confirmation message to the user (obviously, the user won’t see the page, he will be redirected to another one).

这个问题的解决方案是什么?我认识其中两个,但似乎没有一个是完美的.

What are the solutions to this problem? I know two of them, but none of them seems perfect.

  • 使用自定义重定向 URL,例如:http://example.com/?msg=data-saved.它是无状态的,所以我认为它非常可靠.但是当用户复制链接、为它添加书签等时会产生问题.
  • 存储会话变量/cookie,并在每次页面加载时检查它.如果已设置,请将其清除并显示消息.看起来不错,但我不确定这个 - 它强烈依赖 cookie,它有点复杂.
  • Use a custom redirect URL, like: http://example.com/?msg=data-saved. It’s stateless, so I think it’s quite reliable. But it creates problems when user copies the link, bookmarks it, etc.
  • Store a session variable/cookie, and check it on every page load. If it’s set, clear it and display the message. It seems OK, but I’m not sure about this one — it relies strongly on cookies, it’s a little more complicated.

或者也许还有其他我不知道的方式?会话和 URL 参数的某种组合?我不知道.

Or maybe there are other ways I don’t know about? Some combination of sessions and URL parameters? I don't know.

您认为最好的方法是什么?哪一个缺点最少?有什么优点和缺点?

What’s the best way in your opinion? Which one has the least drawbacks? What are the pros and cons?

推荐答案

几乎每个允许用户登录的网站都依赖 cookie.这不是一个完美的解决方案,但它是我们得到的最好的解决方案.

Pretty much every website that allows users to log in does so by relying on a cookie. It ain't a perfect solution, but it's the best we got.

会话处理也是 Web 开发框架通常会为您处理的事情之一.

Also session handling is one of those things that a web development framework typically takes care of for you.

这篇关于如何在 POST + HTTP 重定向后向用户显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆