NextZuck
NextZuck

Reputation: 37

Prevent more than one session from overwriting each other on the same website in PHP

I need more light concerning sessions in PHP, when i have two different users logged in on a particular site lets say for instance their dashboard or account (e.g: https://www.myspace.com/admin/dashboard) in separate tabs in the same browser, one session tends to overwrite the other session, how can i prevent this from occurring.

Upvotes: 0

Views: 533

Answers (2)

Accountant م
Accountant م

Reputation: 7523

The only workaround I could come up with is by using the fact that you can tell the browser to use a cookie only at specific location.

//when user1 visits, set custom session cookie works on his private url
setcookie("sessionId", $sessionTocken1, time() + 3600, "/dashboard/user1"); 

//when user2 visits, set custom session cookie works on his private url
setcookie("sessionId", $sessionTocken2, time() + 3600, "/dashboard/user2");

Now user1 on his dashboard at /dashboard/user1 will send different cookie value than user2 on his dashboard at /dashboard/user2

Please note that by doing this you will have to manage a custom session system at the server side rather than the PHP's $_SESSION builtin one.

I will not doing this for it's weirdness and complex side effects, I just wrote it in case you really need it. Sessions are built on cookies and cookies are not designed to be working for 2 different users using the same browser at the same time.

Upvotes: 0

Adam Hopkinson
Adam Hopkinson

Reputation: 28795

You can't.

Sessions are identified using session cookies and a single browser instance will share cookies across tabs (with the exception of Incognito/Private/etc).

Upvotes: 1

Related Questions