7

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.

Thomas Pedersen
  • 304
  • 3
  • 10
Nikos12071
  • 51
  • 1
  • 1
  • 9
  • 1
    Possible duplicate of [Logout all open tabs automatically when user logs out in one of them](http://stackoverflow.com/questions/13513874/logout-all-open-tabs-automatically-when-user-logs-out-in-one-of-them) – Jay Blanchard Feb 28 '17 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 Feb 28 '17 at 16:30
  • @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 Feb 28 '17 at 16:34

1 Answers1

12

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!

ʇolɐǝz ǝɥʇ qoq
  • 717
  • 1
  • 15
  • 30
DDelgro
  • 463
  • 1
  • 6
  • 16
  • 2
    This assumes `window.onunload` gets called but it is conceivable that it may not; e.g. if the user's computer crashes. Also depending on implementation there could be a race condition, e.g. user opens two tabs. Modern inter-tab communication: https://blog.arnellebalane.com/sending-data-across-different-browser-tabs-6225daac93ec or a heartbeat to server-side code may be required for a robust solution. – Pocketsand May 23 '19 at 17:23
  • Quick note that you don't need the unary `+` operator if you're already doing something arithmetical (like adding or subtracting 1) from the non-numeric value. – Gershom Maes Sep 23 '19 at 17:37
  • 1
    Instead of cookies , local storages are better in this scenario. – Govinda raj Dec 30 '21 at 10:18
  • This is never going to work in production. – MrYellow Jan 18 '23 at 00:27