session_start()的资源使用

发布于 2024-12-05 19:27:19 字数 148 浏览 1 评论 0原文

我使用 $_SESSION 进行用户身份验证,但我也在考虑在会话中存储其他变量。但是,我不知道有多少会话可能使用服务器上的资源。我的意思是在每个页面上都有 session_start() 是否有害,因为即使不需要会话也会启动?

每次就诊时都开始治疗是好是坏还是中立?

I use $_SESSION for authentication of users, but I was thinking about storing other variable in session too. However, I have no idea how much session may use resources on the server. I mean is it harmful to have session_start() on every page, as session will be started even when is not needed?

Is it good or bad or neutral to start session on every visit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你列表最软的妹 2024-12-12 19:27:20

是的,session_start()会消耗资源。但是,除非您的网站有大量流量,否则不会像您注意到的那样。存储会话数据的替代方法几乎都更难实现,并且往往有有趣的故障模式。

我唯一建议的是要小心您在会话中输入的内容 - 即使流量水平较低,大量数据也会产生显着的影响。典型的错误是意外地将业务逻辑层的整个对象树加载到会话状态中。

例如,假设您正在构建一个购物车;当客户点击“添加到购物篮”时,您想要记住他们添加的商品。因此,您可以将商品的唯一 ID 添加到会话中,但是每次显示购物篮时都必须查找价格和描述,这很痛苦。因此,您决定将表示该项目的对象加载到会话中。该对象包含价格和描述,还包含商品的类别 - 以及该类别中的所有其他商品,因为您的应用程序不使用延迟加载。现在,您购物篮中的每件商品还包含数百或数千个其他物品;在您知道自己在哪里之前,您已经将几乎整个数据库加载到会话中。

Yes, session_start() consumes resources. However, unless your site has huge amounts of traffic, not so as you'd notice. And the alternative ways of storing session data are nearly all a lot harder to implement, and tend to have interesting failure modes.

The only thing I'd recommend is to be careful about what you do put into the session - huge amounts of data will have a noticable impact even with lower levels of traffic. The classic mistake is to accidentally load the entire object tree for your business logic layer into your session state.

For instance, assume you're building a shopping cart; when the customer clicks "add to basket", you want to remember the item they added. So, you could add the item's unique ID into the session, but then you have to look up price and description every time you show the basket, which is a pain. So, you decide to load an object representing the item into your session. This object contains price and description, but also the item's category - and all the other items in that category, because your application isn't using lazy loading. So now, each item in your shopping basket also contains hundreds or thousands of other objects; and before you know where you are, you've loaded pretty much your entire database into the session.

谁人与我共长歌 2024-12-12 19:27:19

一般来说,在看到您的网站开始抓取之前,不要担心优化。

在每个页面上使用 session_start() 根本没有害处,事实上,它对于使用身份验证的网站来说是相当标准的。

Generally speaking, do not worry about optimization until you see your website begin to crawl.

Using session_start() on every page is not harmful at all, in fact its pretty standard for websites that use authentication.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文