两个客户的PHP会话重叠

Have a session problem with application when opened in multiple tabs of a browser. In my project a user can have multiple log in id's so he could log into the app with two id's at the same time as two diferent users. but when they try to log in with two id in multiple tabs of a browser. the same session of the browser is being shared and the data gets messed up. Any insights to solve this issue?

I see a pattern in mail.yahoo.com , if i log into my mail.yahoo with one user id and try to login in to other user id in the new tab. one of them logs out. Any idea how this could be done...

Thanks

Piecing this together from against other answers it sounds like you need multiple application streams.

That is, you have a situation where you need multiple "users" to be logged in to the application on different tabs on the same browser, same machine.

This isn't because they are different people using the machine, but rather the same person working with different personas.

It turns out, I've implemented something similar in the past myself, in order for managers to be able to "ghost" through a system as their staff members. They log in as the other user, but in a read only mode so they can see what's going on.

OK. So how to do it.

Put simply - the session isn't enough - you need more than that. The session ID is stored in a cookie on the client machine and there isn't really much you can do about the set-up - one browser = one session.

However, what you can do is split that session up with an application stream, or application context.

That is, don't store anything in the root of your session - split your session into distinct components into which you have a set-up identical to your current session.

The key for each session is then the "application stream" key. You need to pass this around in your URLs.

E.g.

Your current session may have a simple set-up:

$_SESSION['user'] = 'some username';
$_SESSION['role'] = 'power user';

Instead you store that as:

$_SESSION[0]['user'] = 'some username';
$_SESSION[0]['role'] = 'power user';

On all urls you add:

&appId=0

And whenever you reference your session you use something like:

$username = $_SESSION[ $_GET['appId'] ]['user'];

Obviously, you wrap all this up in a nice session handling class, but that's the basic idea.

If you want a link that generates a new login page with a new application stream, you simply change the appId on the link (or completely omit it and trap that in your login code).

E.g.

$sLoginLink = "<a href='/login.php?appId=" . generateNewAppStreamId() . "' target='_BLANK'>New Login Screen</a>";

As everything is still stored in the session, the whole of your application should work exactly the same - just as long as you always have the appId on every URL in the system.

I've tried to make the explanation as simple as possible - forgive me if I've used too many words.

When the user logs out or logs in using a different user ID you must use session_regenerate_id() to force PHP use a different cookie for the new login.

This is actually the best practice on logout.

If you want to have two users logged in simultaneously from the same browser you have to put something in the URL to tell them apart. For example, after login, user #1 will see all the pages as http://www.example.org/1/... and user #2 will have its own customized URL (http://www.example.org/2/...). Then you need to use session_set_cookie_params() for each user with the correct value for parameter $path ('/1' for user #1, '/2' for user #2 and so on).

It's not recommended to use the user ID as customized user directory but to generate a hash from it.

If you want to use session then you must arrange such mechanism that only one user can be logged in same browser. At login page, check availability of session and it is already have a value than redirect your page to any logged in page like home, profile or whatever you have.