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.
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.
Share Improve this question edited May 15, 2017 at 9:30 Thomas Pedersen 3043 silver badges10 bronze badges asked Feb 28, 2017 at 16:20 Nikos12071Nikos12071 511 gold badge1 silver badge9 bronze badges 3- 1 Possible duplicate of Logout all open tabs automatically when user logs out in one of them – Jay Blanchard Commented Feb 28, 2017 at 16:27
- I think your idea is bad, for example the unload doesn't will run if the user close his laptop, or freeze the browser. I think the best is if you update a users "lastclick" timestamp in database. – esemve Commented Feb 28, 2017 at 16:30
- 1 @esemve if user is idle and don't do anything session is closing and database is updating. This is a different case that i have took care off already. – Nikos12071 Commented Feb 28, 2017 at 16:34
1 Answer
Reset to default 12This 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!