1

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.

1
  • "If id is specified, it will replace the current session id. session_id() needs to be called before session_start() for that purpose." php.net/manual/en/function.session-id.php Commented Nov 23, 2015 at 22:16

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, I will have to make it work out that way. Thanks a lot!
if it's really required. is the most important line in this answer.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.