Forum Moderators: coopster

Message Too Old, No Replies

Multiple Sessions

Managing different variables in different sessions

         

migthegreek

11:38 am on May 12, 2009 (gmt 0)

10+ Year Member



I'm not really sure how to go about this (or if it's possible), but I have a need to keep different groups of session variables that correspond to separate things.

For example, I have a bunch of variables that store info about the currently logged-in user. Then I would like to store some session variables about the environment, but keep them separate from the user session variables. This would enable me to do things like loop through JUST the user session variables, or JUST the environment session variables. Also, I could destroy all user session variables, without affecting the environment variables.

At the moment, I'm just prepending some sort of identifier to each variable key, such as 'user_' to every user session key (e.g. 'user_firstname') to keep them identified, but I think this is pretty stupid, and assume multiple sessions can be kept separate... I just couldn't find any helpful information out there. Can I do this with session names?

I don't want to have to worry about giving every session variable a unique prefix, for fear of deleting unrelated session variables. For example, on a form I might like to have a temporary session variable called 'email', which I would delete at the end of the form. But there's already an 'email' session variable for the currently logged-in user, so I have to call it 'user_email' to identify it separately. It would be nice to store the temporary form data in a separate session that I can just delete outright and not worry about the other sessions.

enigma1

1:03 pm on May 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One way you could do this is by setting a database table to store the various session information. So you could say have one db table that lists for each session id the various parameters (one parameter can be used to flag if a user is logged-in). Then you just pass the session id with the cookie. You don't need to convey every detail for each session parameter in-between page transition.

A session table can simply consist of a couple of columns. One can hold the id the rest the session specifics. The information stored in the db can be serialized.

You can check the sessions reference in php.net

migthegreek

1:56 pm on May 12, 2009 (gmt 0)

10+ Year Member



Well I think I found the answer to my question anyway. As far as I could establish, there's no way to have different sessions simultaneously.

So what I did was just store everything in arrays in $_SESSION.

e.g.

$_SESSION['user'] = array(); // Stores all user session vars
$_SESSION['something-else'] = array(); // Stores other stuff

And this way I can partition the session variables and just unset one of those arrays.