Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Session per Tab

Tags:

php

tabs

session

Is it possible to make sessions per Browser tabs?

As example a user opened 2 tabs in his browser: Tab 1 and Tab 2

In Tab 1 he has a session:

$_SESSION['xxx'] = 'lorem';

And in Tab 2 the session is:

$_SESSION['xxx'] = 'ipsum';

Now on refresh i need to get the current session in the active tab. For example if the user refreshes Tab 2 i need to get the $_SESSION['xxx'] for tab 2 on load which is 'ipsum'. But $_SESSION['xxx'] shouldn't change on Tab 1.

Is there any option to save sessions per tab. If not what are other options to handle this issue?

Thanks for any help!

like image 361
lhuber Avatar asked Jan 15 '16 09:01

lhuber


People also ask

How many sessions can PHP handle?

1000+ sessions can still be perfectly handled by standard PHP file based sessions. If you notice that is getting a problem, you can exchange the session backend easily. There are pluggable session handlers for memcached or other memory or database based storage systems.

How can I set session on all pages in PHP?

To handle session, you must first start it and store some value to any session variable. You can create any amount of session variable you wish. To validate whether Session is active or not, we use isset() function and finally to destroy it we use unset() function.

How long is a PHP session?

By default, a session lasts until the user closes his browser. This option can be changed in the php. ini file on the web server by changing the 0 in session. cookie_lifetime = 0 to the number of seconds you want the session to last or by using session_set_cookie_params().


2 Answers

PHP stores session IDs in cookies and cookies are per client (browser), not tab. So there is no simple and easy way to do this. There are ways of doing this by creating your own session handlers, but they are more hacks than solutions and as such come with their own risks and complexity. For whatever reason you may need this, I am quite sure that there is a better architectural solution than session splitting.

like image 191
Auris Avatar answered Sep 16 '22 11:09

Auris


I've been scouring the web for answers to this problem and have not yet found a satisfying solution. I finally pulled together something in JavaScript that sort of works.

//generate a random ID, doesn't really matter how    
if(!sessionStorage.tab) {
    var max = 99999999;
    var min = 10000000;
    sessionStorage.tab = Math.floor(Math.random() * (max - min + 1) + min);
}

//set tab_id cookie before leaving page
window.addEventListener('beforeunload', function() {
    document.cookie = 'tab_id=' + sessionStorage.tab;
});

HTML5 sessionStorage is not shared between tabs, so we can store a unique tab ID there. Listening for the beforeunload event on the window tells us that we're leaving (and loading some other page). By setting a cookie before we leave, we include our value in the new request without any extra URL manipulation. To distinguish between tabs you just have to check $_COOKIE['tab_id'] on the server and store sessions values appropriately.

Do note that Firefox behaves strangely, in that triggering window.open() will create a window that shares sessionStorage with its parent, giving you two tabs with the same ID. Manually opening a blank tab and then navigating to the target URL will give you separate storage. Chrome works for me in all of my tests so far.

I realize this is probably not the right answer, or even a "good" answer, but it is an answer.

like image 37
allknowingfrog Avatar answered Sep 16 '22 11:09

allknowingfrog