views:

53

answers:

3

I have a project where I would like to create two session cookies in one browser. The first session would be to uniquely identify a person, the second would be to share events within the session between users. I have been using a database for this, but would like the data to disappear when the session dies. There are no logins within the system.

Is there a way to do this, other than creating a cookie system to replicate functionality?

For example, we would have two session cookies:

name=someRandomUUID and session=valueSharedBetweenUsers.

I don't want to share the name session with multiple users, but the session session would be. Thoughts?

+3  A: 

If you want to share information between users, using a session is not the best idea as it uses the file system. You would be better off using the database which handles all the issues of locking, concurrency etc.

Although what you ask for is technically possibly, I would strongly recommend against it.

EDIT

Assuming I have understood your requirement correctly, here is how I would do it:

  1. Use session only to store session data related to that user. It could include something like:

    $_SESSION['name'] = 'test name'; $_SESSION['groupid'] = 2;

  2. A mySQL db and table with field group id, XXXXX (data you want to store), timestamp

Whenever anyone updates information for a particular group id, you update the timestamp

Then run a simple cronjob to check if any current time - timestamp > 3600 (one hour) and you can consider that as stale and delete those records.

Alec Smart
Could you please expand on use of the file system being bad?
@user: If two users hit the site at the exact same time, there's the possibility of both processes writing the shared session at the exact same time. If that happens, at the very least one user's changes will be ignored, and in the worst case the session file will be corrupted.
cHao
I'd like to avoid using a database as a solution, I'm doing that already. I entirely agree it's a better way and that's not what sessions are for, but this is really just to try something different: multiple session management, similar in functionality to $_SESSION, just allowing for multiple sessions.
+1  A: 

I *think* you can only have one "current" session, but the functionality you are referring to is session_name:

http://www.php.net/manual/en/function.session-name.php

The cookie functionality is very simple. I suggest looking into that instead.

Docunext
+1  A: 

Where is the "valueSharedBetweenUsers" coming from? Is it a constant or database entry?

Either way, it wouldn't make sense to create one session per group. You should instead be giving each user a unique session per user; with your "shared" attribute as a session attribute for each individual.

So start the unique session then just do <? $_SESSION['session'] = 'mySharedValue'; ?>

Now everyone has a session with a unique sessionID and a common value 'session'.

(Obviously if you need to change this attribute later you'll have to do it separately for each authed individual)

pws5068
The value could be users that enter the same word into an input, for example, and start a session named "apples" where both users can `print_r($_SESSION)` and post data into the array. The users would also have individual controls over what sessions they are in, as an example of why a private session would be required.