Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  114] [ 5]  / answers: 1 / hits: 22440  / 7 Years ago, tue, february 28, 2017, 12:00:00

I am new in JavaScript and php so I have a question of if there is a way to detect multiple tabs or windows of the same session in a website on a browser.
On the website I am making, when a user logs in, a boolean (0-1) variable is stored on a table of users in a database, that shows if that user is logged in. After someone logs and close tab or browser's window, the database is updating once again its changing the variable that shows if the user is logged in. The way I am doing that is by just calling the script bellow



$(window).bind(beforeunload,function(){
$.ajax({
url: logout.php,
async: false
});
})


The logout.php updates the database if the user closes window or tab. My problem is, if someone has more than one tabs open with the same session of the same website, and just close one of them the session will remain while the database is going to update that the user is not logged anymore.



EDITED: I don't want to logout all opened tabs, I just wan't to keep on the database that the user is still online until he closes all tabs-windows of his session or hit the logout button.


More From » php

 Answers
5

This is from a previous stack overflow post:



if (+Cookies.get('tabs') > 0)
alert('Already open!');
else
Cookies.set('tabs', 0);

Cookies.set('tabs', +Cookies.get('tabs') + 1);

window.onunload = function () {
Cookies.set('tabs', +Cookies.get('tabs') - 1);
};


URL:
How to know if browser tab is already open using Javascript?



This should be useful to you!



*This is not my code, just passing on information!


[#58739] Sunday, February 26, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jennifer

Total Points: 517
Total Questions: 110
Total Answers: 104

Location: New Caledonia
Member since Fri, Sep 11, 2020
4 Years ago
;