views:

56

answers:

2

Ok, I am storing a session variable like so to load up users layouts faster if it's set instead of calling the database. But since the layout can be changed via the Administrator, I'd like to be able to globally remove all sessions where $_SESSION['layout']['action'] is set for all users.

$_SESSION['layout']['action'] = array(a ton of indexes and mulit-dimensional arrays);

Now, I know it's being stored into my database sessions table, there's a column for session_id, last_update, and data. So, question I have is how to remove that session array key ['action'] from all users.

Using

$_SESSION = array();
session_destroy();

Does not work. Basically, session_start() is being loaded on every page load, so I just want to remove all ['action'] keys from ['layout'].

Is this possible to do? Thanks

+5  A: 

Ok, I am storing a session variable like so to load up users layouts

wrong

I'd like to be able to globally remove all sessions where

wrong

it's being stored into my database

OMG "t instead of calling the database"!

Is this possible to do? Thanks

Leave sessions alone and don't use it for the global settings.

Col. Shrapnel
+1 Harsh, but the man has a point :)
Pekka
Ok, sure. Good Point.
SoLoGHoST
Do you have any idea on how to use a global variable across multiple files in php than? Thanks.
SoLoGHoST
@sologhost a lot. Store it in the database.
Col. Shrapnel
wow, duh, ok, thanks for that bit of info than... hehe, really feeling stupid now. The way you reply to these threads from me have me cracking up at myself! Cheers :)
SoLoGHoST
But storing it in the database sort of defeats the purpose, since I'm already grabbing the info from the database to fill it up once. The thing is, I don't want to have to keep filling up the array if it's already set by calling the database query every single time. Though I want to be able to unset it for certain functions within a certain file for all members. Well, perhaps you're right, grabbing from the database at all times is the best way to go than...
SoLoGHoST
@sologhost you just messed up purposes. Session purpose is to store **personal data**. Database purpose is to serve requests. there is not a single reason to pull the data "once" and then pull it from somewhere else. Every storage are generally equal. So, you just don't need to take your data from one storage to another. Database is ok, it is intended to query it every single time
Col. Shrapnel
A: 

If you don't want to hit the database each time to load configuration data, you can cache it in a generated .inc file. Remember, PHP is just text - you can use a PHP script to generate another PHP script:

$fh = fopen('sitevars.inc'); // skipping error handling, since this is an example.
fwrite($fh, '<' . '?php' . "\n"); // split the <? tags in case an unbalanced ' somewhere hoses things
fwrite($fh, '$lifetheuniverse = 42;' . "\n"); // single quotes to the $ doesn't have to be escaped.
fwrite($fh, "\$layoutaction = 'slap forehead with palm';\n");
fclose($fh);

and then you just include_once('sitevars.inc'); and boom, it's a "global" variable. No messing with sessions.

That being said, if your sessions are being stored in the database, most likely they're in serialized format. To do a proper job of stripping a particular "global" session var from each, you'd have to load each record, de-serialize, delete the variable, re-serialize, and re-save into the DB. And hope you don't trash someone's session who happens to be active at the time you're doing these updates.

Marc B
Hmmm, that's an interesting approach. Thank You, will definitely consider doing this. Does it have to be an .inc file? Would it be fine as just another php file instead?Cheers :)
SoLoGHoST
I tend to use .inc files for storing vars and object definitions, etc.. and store them somewhere outside the webroot. But otherwise, no. No reason it can't be a .php or .whateverelse. It's just plain text, after all.
Marc B