简体   繁体   English

我可以在PHP中同时进行多个会话吗?

[英]Can I have multiple sessions at the same in PHP?

I have a project and I would like to know if it's possible to have two sessions at the same time. 我有一个项目,我想知道是否可以同时进行两次会议。

Eg 例如

I have a user which is a football player (he has a log in) and I want him to log in on a team to know the updates. 我有一个足球运动员用户(他已经登录),我希望他登录一个团队以了解最新信息。

I was thinking about something like this: 我在想这样的事情:

session_id("player");

session_start();

session_id("team");

session_id("player");

This gives me an error that sais that a session had already been started. 这给我一个错误,表明会话已经开始。

I would like to know if it's possible to have two sessions at the same time. 我想知道是否可以同时进行两次会议。

Well... no. 好吧 You can't have multiple simultaneous sessions. 您不能同时进行多个会话。 But you can start one, then close it with session_write_close and start another session... if it's really required. 但是您可以启动一个会话,然后使用session_write_close关闭它,然后启动另一个会话...( 如果确实需要)。

session_id("player"); // are you sure that you don't want use `session_name` here?
session_start();

// ... work with session id "player"

session_write_close();

session_id("team");
session_start();

// ... work with session id "team"

Also, you may want use session_name instead of session_id . 另外,您可能需要使用session_name而不是session_id One sets the current session name, the other the SID. 一个设置当前会话名称,另一个设置SID。

It is probably a bit late for the answer but I had the same issue and I solved it with two dimensional arrays. 答案可能为时已晚,但是我遇到了同样的问题,并使用二维数组解决了这个问题。 In your case it would be something like this: 在您的情况下,将是这样的:

Login into system: 登录系统:

$_SESSION['userid'] = 10
$_SESSION['username'] = 'XYZ';

Now if the user clicks on a team, which has the id 20 than the session could look like this: 现在,如果用户单击一个ID为20的团队,则该会话将如下所示:

$_SESSION['userid'] = 10
$_SESSION['username'] = 'XYZ';
$_SESSION['team'][20]['teamname'] = 'ABC';
$_SESSION['team'][20]['SOME_TEAM_SPECIFIC_PROPERTY'] = 'SOME_TEAM_SPECIFIC_VALUE';
...

This would make it possible being logged into more then one team at the same time for instance if you have opened more than one tab etc... 例如,如果您打开了多个标签等,则可以同时登录多个团队。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM