I am writing an algorithm which will produce some values that I'd like stored in session variables for easy retrival in multiple blocks.
The method I have been using so far is just using $_SESSION['var'] variables.
However, I was looking into variable_set() and variable_get() and I think those kind of do the same thing.
The thing is, the variables I am creating with my algorithm must be unique to each individual end user who visits my site. It seems variable_get() is intended for variables which will apply for ALL users, like form information.
So I am wondering, what is the "Drupal" way of storing session information for individual users? (Or is the $_SESSION variable method the way to go?)
Thanks
Comments
$_SESSION is temporary, variable_set() is persistent
The way I understand it, $_SESSION values are session-specific since they last only as long as the user's session. variable_set() is used for module and site configuration variables. If the variables you want to use in your blocks are intended to last only for the user's current session, then use $_SESSION['var']. If you want values to persist across sessions, you'll probably need to create a module that uses its own table to store user variables.
Cool thanks, quick follow up
Cool thanks, quick follow up question though:
How long are sessions set to last for? Till the user leaves the site or after a set amount of time?
EDIT: To answer my own question, this session period is set in the settings.php file.
ini_set('session.cache_expire', 200000);Code for managing session data...
I wrote some code which I've been considering packaging as a module... enclosed herein. This code gives anonymous users the benefit of "remember me" and registered users "remember me, indefinitely, from any computer." You can see it in action at www.authorcollector.com.
- If a user is registered (has a non-zero UID), preferences are stored in a database (in the code below, database 'user_preferences').
- Preferences for unregistered users are stored in a session variable $_SESSION['my_module'].
- Data is serialized into a single value field, so no prior knowledge of field names or structure is required.
- If a value is retrieved which has not been set, you'll get an empty string. I suppose this could be improved to support defaults or changed to return a NULL.
- Note this code makes no attempt to sanity check the input (no check_plain)... this is left up to the client.
Usage:
1) Getting a single session variable...
2) Getting multiple session variables...
3) Setting one or more session variables...
Here is the code... I generally package this as 'user.prefs.inc' and then include it in files which manage preferences.
And the following is the code in your module's install file... "my_module.install"...
Wow that looks awesome. I'd
Wow that looks awesome.
I'd be very interested in seeing that as a module.
Do you have a project page, if I get a chance I could look into it and offer a hand if I can.
No project page (yet)...
Thanks for the positive feedback... :)
Nope, no projects page. But probably about time for me to take that step... I suspect this would be useful as a module.
Can you wait a day or two? Might take me that long to post something which wouldn't embarrass me.
Mark
PS. I may even have undersold this a bit. The following works... a more complex example of user preferences...
Outputs...
Curious if you packaged
Hey Mark,
I'm curious if you ever packaged your code into a module. It's nice and looks like it could help a lot of people out. Seems like an easy upkeep to for future core releases.
Best,
Milo
Wow. This is GREAT! I've
Wow. This is GREAT!
I've been searching for a concise example of using the Drupal DB outside of the context of creating a custom content type, and this has everything I've been looking for...
-Blake
www.BlakeSenftner.com www.3D-Avatar-Store.com
Sued you code in my own
Used you code in my own custom module. Sadly, somehow people seem to get mixed up. I use the "module" to safe user preferences, but somehow these will be mixed sometime between users....
Any idea why?
Mark
Dinilu.eu & Dinilu.nl & Dinilu.de & www.tit.nl