Multiple sessions in one instance using PHP?

前提是你 提交于 2019-12-04 01:52:44

问题


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?


回答1:


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 fields groupid, 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.




回答2:


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.




回答3:


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)




回答4:


This isnt as far fetched as people are making facebook and twitter have at least 10 different sessions being created when a user has logged in.



来源:https://stackoverflow.com/questions/3602476/multiple-sessions-in-one-instance-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!