PDA

View Full Version : Additional session variable: best way to store and retrieve?


mfyvie
06-30-2007, 05:03 PM
I'm trying to store and reuse a value that should be valid for the entire session, whether a logged-in user or a guest. This value would then be available for use on any page loaded during that session.

Could someone perhaps point me to the most efficient way to do this? I started looking at the vB_Session class which seemed to have some promising methods, but then I realised that these are hard-coded for the existing fields in the session table.

Should I simply put something in the datastore? But if I put something in the datastore how will I be sure that it cleans out after the session expires? On re-reading the article about the datastore and taking a look at it, it appears that it isn't really designed for storing data associated with a session.

I apologise if this sounds like I'm asking too many questions - but I have been reading through the vB code documentation and various tutorials all day. This is sort of along the lines of what I want:

at global_start:

if (!isset($userinfo['foo'])) {
$userinfo['foo'] = "Our value to remain for the entire session";
// now we need to save it somehow so when the next page
// is loaded it is still here, and this conditional doesn't execute again
}Any help would be greatly appreciated! (this question is unrelated to my other thread about adding a field in the admincp by the way)

Incendium
06-30-2007, 10:24 PM
Why not use $_SESSION?

mfyvie
07-01-2007, 06:35 AM
Why not use $_SESSION?

I wondered about this, but when I did a grep through the vbulletin files, I couldn't find a single occurrence of $_SESSION. I know the vbulletin code standards (http://www.vbulletin.com/docs/html/codestandards_gpc) tell us not to use superglobals like $_POST, $_GET, etc, so is using $_SESSION somehow a bad thing? Is there a better way, and how does vb do it?

Dismounted
07-01-2007, 07:14 AM
vBulletin adds things to the session table. Note "inforum", etc. Just insert a field into that table and update it, vBulletin will handle the cleaning up. They are then accessed via $vbulletin->session->vars.

mfyvie
07-01-2007, 07:22 AM
vBulletin adds things to the session table. Note "inforum", etc. Just insert a field into that table and update it, vBulletin will handle the cleaning up. They are then accessed via $vbulletin->session->vars.

Yes, I did look at the session table and the various mechanisms initially, but wondered if I would be adding too much overhead by extending the session table. But I guess you are right, it is the most efficient way to do it, and since this table gets cleaned up, I guess it's the right way to go.

I just wanted to make sure I do this in the most efficient way possible. Thanks for the tip.