在 Web 应用程序中检测同一会话的多个选项卡或窗口 [英] Detect multiple tabs or windows of the same session in a web application

查看:31
本文介绍了在 Web 应用程序中检测同一会话的多个选项卡或窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JavaScript 和 php 的新手,所以我想知道是否有办法在浏览器的网站中检测同一会话的多个选项卡或窗口.在我制作的网站上,当用户登录时,一个布尔 (0-1) 变量存储在数据库中的用户表中,显示该用户是否已登录.有人登录并关闭选项卡或浏览器窗口后,数据库再次更新它改变显示用户是否登录的变量.我这样做的方法是调用下面的脚本

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
    });
})

如果用户关闭窗口或选项卡,logout.php 会更新数据库.我的问题是,如果有人在同一个网站的同一个会话中打开了多个选项卡,并且只需关闭其中一个,会话将保留,而数据库将更新用户不再登录.

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.

推荐答案

这是上一篇堆栈溢出的帖子:

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);
};

网址:如何知道浏览器标签是否已经打开使用Javascript?

这应该对你有用!

*这不是我的代码,只是传递信息!

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

这篇关于在 Web 应用程序中检测同一会话的多个选项卡或窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆