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

查看:33
本文介绍了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 中,还是基于隐藏在表单字段中的数据重建它们,或者每次都从数据库中重新查询它们,您都在使用状态.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天全站免登陆