Cookie与基于会话的Flash消息 [英] Cookie vs. Session based flash message

查看:70
本文介绍了Cookie与基于会话的Flash消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CakePHP中发现的一个简洁功能是能够设置 flash 消息的功能,例如在某些保存上说脚本,然后在下一页上显示该消息。 发布已更新,或错误-找不到文件。

A neat feature which I found in CakePHP was the ability to set a flash message, say on some save script, then have that message displayed on the following page. Something like, Post updated, or Error - no file found.

Cake的工作方式与此 session 对象有关。我试图避免像瘟疫这样的会议,因为它们对可伸缩性的要求很奇怪。我不仅可以将Flash消息存储在Cookie(客户端)中,然后将其显示在下一页上,然后将其删除吗?这种方法的利弊是什么?或更简单地说,为什么Cake使用 session (我假设这与 _SESSION 收藏)。

The way Cake does it is with this session object. I am trying to avoid sessions like the plague because of their odd requirements for scalability. Can I not just simply store the flash message in a cookie (client side) and then delete that cookie once it's displayed on the following page? What would be some pros/cons to this approach - or more simply, why does Cake uses session (I'm assuming that relates to the _SESSION collection).

欢呼!

ps在我的实现中,我还使用javascript中的 setTimeout 命令使其淡出。我发现这是结束整个过程的好方法。

p.s. In my implementation I also make it fade out with a setTimeout command in javascript. I find that's a nice way to end the whole process.

推荐答案

Cookie的问题在于用户可能禁用了此功能。如果是这样,则不会显示您的Flash消息。 CakePHP尽量变得通用,并使用会话存储。

The problem with a cookie is that the user may disable this functionality. If so, your flash message won't be showed. CakePHP try to be general enough and uses session storage.

您有3个选择:


  1. 会话:最常用的方法。它可以在任何客户端计算机上运行,​​但是,正如您所说的,它可能会使某些服务器配置出现问题。

  2. Cookies :通常,这是一个不错的选择,但是用户可以阻止此机制。仅当您的应用程序要求包含cookie时才建议使用。

  3. 数据库:通用解决方案。问题在于它需要访问数据库(慢)。应该使用URL(GET方法)传递一个ID,以便应用程序知道哪个数据库寄存器与此访问相对应。

  1. Session: the most used approach. It will work in any client computer but, as you say, it could give problems with some server configurations.
  2. Cookies: it's a good option in general, but the user may block this mechanism. Only recommendable when the your app requirements include the need of cookies.
  3. Data base: the universal solution. The problem is that it requieres an access to the database (slow). An ID should be passed with the URL (GET method) so the application knows which database register corresponds to this access.

在我的应用程序中我将第二种方法和第三种方法结合使用:我测试cookie,如果可用,我会使用它们。如果没有,我将使用数据库访问权限,但是我总是缓存数据库访问权限,以便不对每条消息查询一次以上。

In my applications I use a combination of the 2nd and 3rd approaches: I test for cookies and if they are available, I use them. If not, I use database access, BUT I always cache the DB access in order to not query more than once for each message.

这篇关于Cookie与基于会话的Flash消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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