PHP:在$ _SESSION中存储“对象" [英] PHP: Storing 'objects' inside the $_SESSION

查看:103
本文介绍了PHP:在$ _SESSION中存储“对象"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现我实际上可以将对象存储在$ _SESSION 中,我发现它很酷,因为当我跳到另一页时,我仍然有我的对象.现在,在我开始使用这种方法之前,我想知道这是否真的是一个好主意,或者是否涉及到潜在的陷阱.

I just figured out that I can actually store objects in the $_SESSION and I find it quite cool because when I jump to another page I still have my object. Now before I start using this approach I would like to find out if it is really such a good idea or if there are potential pitfalls involved.

我知道,如果我只有一个入口点,那么我就不需要这样做,但是我还没有,所以我还没有一个入口点,我真的很想保留我的对象,因为我不会那样失去自己的状态. (现在我也读过我应该对无状态站点进行编程,但是我还不了解这个概念.)

I know that if I had a single point of entry I wouldn't need to do that but I'm not there yet so I don't have a single point of entry and I would really like to keep my object because I don't lose my state like that. (Now I've also read that I should program stateless sites but I don't understand that concept yet.)

所以简而言之:可以在会话中存储对象,这有什么问题吗?

So in short: Is it ok to store objects in the session, are there any problems with it?

临时摘要:到目前为止,我了解即使重新涉及查询数据库,重新创建对象可能也是更好的对象.

Temporary summary: By now I understand that it is probably better to recreate the object even if it involves querying the database again.

进一步的答案可能会在这方面进行详细说明

推荐答案

我知道这个话题很老,但是这个问题不断出现,并没有得到我的满意解决:

I know this topic is old, but this issue keeps coming up and has not been addressed to my satisfaction:

是将对象保存在$ _SESSION中,还是基于隐藏在隐藏表单字段中的数据将它们整块地重建,还是每次都从DB中重新查询它们,您都在使用状态. HTTP是无状态的(或多或少;但是请参阅GET与PUT的比较),但是几乎任何人关心与Web应用程序相关的所有事情都需要在某个地方维护状态.好像把国家推到了角落,这在某种程度上是理论上的胜利,这是错误的.状态就是状态.如果使用状态,则将失去无状态所带来的各种技术优势.除非您事先知道自己应该因此而失去睡眠,否则这并不是失去睡眠的原因.

Whether you save objects in $_SESSION, or reconstruct them whole cloth based on data stashed in hidden form fields, or re-query them from the DB each time, you are using state. HTTP is stateless (more or less; but see GET vs. PUT) but almost everything anybody cares to do with a web app requires state to be maintained somewhere. Acting as if pushing the state into nooks and crannies amounts to some kind of theoretical win is just wrong. State is state. If you use state, you lose the various technical advantages gained by being stateless. This is not something to lose sleep over unless you know in advance that you ought to be losing sleep over it.

汉克·盖伊(Hank Gay)提出的双重打击"论点所带来的祝福使我尤其感到困惑. OP是否正在构建分布式且负载均衡的电子商务系统?我的猜测不是.而且我将进一步假设,序列化他的$ User类或任何其他方法不会使他的服务器瘫痪而无法修复.我的建议:使用对您的应用程序明智的技术. $ _SESSION中的对象可以使用,但要遵循常识性的预防措施.如果您的应用突然变成了可以与亚马逊媲美的流量,那么您将需要重新适应.那就是生活.

I am especially flummoxed by the blessing received by the "double whammy" arguments put forth by Hank Gay. Is the OP building a distributed and load-balanced e-commerce system? My guess is no; and I will further posit that serializing his $User class, or whatever, will not cripple his server beyond repair. My advice: use techniques that are sensible to your application. Objects in $_SESSION are fine, subject to common sense precautions. If your app suddenly turns into something rivaling Amazon in traffic served, you will need to re-adapt. That's life.

这篇关于PHP:在$ _SESSION中存储“对象"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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