Jump to content

[SOLVED] Storing Data in Session


premiso

Recommended Posts

Hello All,

 

Have a question about storing data in session. I have a blog site with users of course. Instead of having to make a call to getUserData() each time I want user data inside a function (or defining it globally) I thought about storing the data in a session variable and doing one of two things. Keeping that throughout the lifetime of the session, and only re-querying if a change has been made since the request was pulled (Date field in the user table of last_modified) or just storing it in session and un setting the data each page.

 

Not sure which (if any) is better. Most likely there will be about 20 fields stored, username, email, and some options the user is able to set which should only be boolean variables. I think storing it in session and testing against the timestamp would work great but before I start coding this for my whole site would like to know some input.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/62593-solved-storing-data-in-session/
Share on other sites

The cost of pulling an entire row compared to pulling one column from the database is not much different.  Not unless there is a LOT of data in that row.  You may save some time in parsing the data though.  I would expect you will see little difference though.

 

But I could be wrong :)  It's always difficult to predict performance.

 

Another option is to store the data in a session, and let the script itself refresh the data only if the script itself makes a change to it.  That means that if some other script modifies the user's data, the change won't show up.  But you do save the trouble of checking to see if it's updated.  Whether or not that works depends on your situation.

I guess either way I am making a trip to the database. The goal is to have the options available globally (via session). So I guess checking against the timestamp works and just keep the data in session will be how I go. It will definitely be under 5KB so I doubt there will be a problem.

 

Thanks for the input bud.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.