开发者

Resource usage of session_start()

I use $_SESSION for authentication of users, but I was thinking about stori开发者_Python百科ng 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜