RaulG3
RaulG3

Reputation: 65

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.

E.g.

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.

Upvotes: 1

Views: 3900

Answers (2)

quinlan
quinlan

Reputation: 122

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:

$_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

Upvotes: 1

Federkun
Federkun

Reputation: 36999

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_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. One sets the current session name, the other the SID.

Upvotes: 3

Related Questions