I have a web app game and while in the game I want to have it so if a user closes the page or their browser, it will automatically log them out. I tried using the onbeforeunload event attached to the window:
我有一个网络应用程序游戏,而在游戏中我想拥有它,所以如果用户关闭页面或他们的浏览器,它将自动登出。我尝试使用附加到窗口的onbeforeunload事件:
window.onbeforeunload = function() {
// perform logout functions here
}
The problem is, that will also fire if the user refreshes the page. Is there a way I could detect whether or not the user is completely closing the whole page, or just refreshing it?
问题是,如果用户刷新页面,也会触发。有没有办法可以检测用户是否完全关闭整个页面,或只是刷新它?
4 个解决方案
#1
There is not a detectable difference. To automatically logout a user, you should set an expiration on your cookie storing the login or session information. So if you set it for 1 hour, the user would essentially be logged out after that time since the cookie would be destroyed. If you wanted to postpone this auto logout while they are still interacting with the site, you could reset the expiration of the cookie every time they perform some sort of action (clicking a link, activating an AJAX call, etc). That would mean that they'd be logged out after 1 hour of inactivity as opposed to just 1 hour from login, which sounds more like what you want.
没有可检测到的差异。要自动注销用户,您应该在存储登录或会话信息的cookie上设置到期日期。因此,如果您将其设置为1小时,则该用户将在该时间之后注销,因为cookie将被销毁。如果您希望在他们仍然与网站交互时推迟此自动注销,则可以在每次执行某种操作(单击链接,激活AJAX调用等)时重置cookie的到期时间。这意味着他们将在1小时不活动后退出登记,而不是登录后1小时,这听起来更像你想要的。
If you set the cookie's expiration to 0, then it will expire it after the session ends. That usually occurs when the user quits their browser entirely. That's another option as well.
如果将cookie的到期时间设置为0,那么它将在会话结束后使其到期。这通常发生在用户完全退出浏览器时。这也是另一种选择。
#2
As said, you cannot. Even worse, this event have been abandoned by lot of browsers, probably because it have been abused by malicious scripts doing pop-under and such.
如上所述,你不能。更糟糕的是,这个事件已经被很多浏览器抛弃了,可能是因为它被恶意脚本滥用了。
A possible workaround is to have an Ajax script "phoning home": if it is silent for some time, the user just abandoned the site (closed page or browser).
一个可能的解决方法是让Ajax脚本“打电话回家”:如果它静默了一段时间,用户就放弃了网站(关闭的页面或浏览器)。
#3
Have the onunload event send a request to the server which will cause the session to expire in n seconds (where n is the maximum time for a page reload request to occur, so perhaps 10). Then have the script for the site check to see if that event is scheduled and if so, cancel it. This would give you the behavior you seem to want.
让onunload事件向服务器发送请求,这将导致会话在n秒内到期(其中n是页面重新加载请求发生的最长时间,因此可能是10)。然后让站点的脚本检查是否已安排该事件,如果是,请取消它。这会给你你想要的行为。
But yeah, I'd recommend simply having the session expire.
但是,是的,我建议只是让会议到期。
#4
If I'm not mistaken Javascript should have a function named something like onWindowClose, maybe try searching for it?
如果我没有弄错,Javascript应该有一个名为onWindowClose的函数,也许可以尝试搜索它?
Regarding PHP solutions, I'm not sure if there are any but I suggest you take a quick look into PHP Connection Handling, specifically the connection_aborted() and register_shutdown_function() functions.
关于PHP解决方案,我不确定是否有,但我建议您快速了解PHP连接处理,特别是connection_aborted()和register_shutdown_function()函数。
#1
There is not a detectable difference. To automatically logout a user, you should set an expiration on your cookie storing the login or session information. So if you set it for 1 hour, the user would essentially be logged out after that time since the cookie would be destroyed. If you wanted to postpone this auto logout while they are still interacting with the site, you could reset the expiration of the cookie every time they perform some sort of action (clicking a link, activating an AJAX call, etc). That would mean that they'd be logged out after 1 hour of inactivity as opposed to just 1 hour from login, which sounds more like what you want.
没有可检测到的差异。要自动注销用户,您应该在存储登录或会话信息的cookie上设置到期日期。因此,如果您将其设置为1小时,则该用户将在该时间之后注销,因为cookie将被销毁。如果您希望在他们仍然与网站交互时推迟此自动注销,则可以在每次执行某种操作(单击链接,激活AJAX调用等)时重置cookie的到期时间。这意味着他们将在1小时不活动后退出登记,而不是登录后1小时,这听起来更像你想要的。
If you set the cookie's expiration to 0, then it will expire it after the session ends. That usually occurs when the user quits their browser entirely. That's another option as well.
如果将cookie的到期时间设置为0,那么它将在会话结束后使其到期。这通常发生在用户完全退出浏览器时。这也是另一种选择。
#2
As said, you cannot. Even worse, this event have been abandoned by lot of browsers, probably because it have been abused by malicious scripts doing pop-under and such.
如上所述,你不能。更糟糕的是,这个事件已经被很多浏览器抛弃了,可能是因为它被恶意脚本滥用了。
A possible workaround is to have an Ajax script "phoning home": if it is silent for some time, the user just abandoned the site (closed page or browser).
一个可能的解决方法是让Ajax脚本“打电话回家”:如果它静默了一段时间,用户就放弃了网站(关闭的页面或浏览器)。
#3
Have the onunload event send a request to the server which will cause the session to expire in n seconds (where n is the maximum time for a page reload request to occur, so perhaps 10). Then have the script for the site check to see if that event is scheduled and if so, cancel it. This would give you the behavior you seem to want.
让onunload事件向服务器发送请求,这将导致会话在n秒内到期(其中n是页面重新加载请求发生的最长时间,因此可能是10)。然后让站点的脚本检查是否已安排该事件,如果是,请取消它。这会给你你想要的行为。
But yeah, I'd recommend simply having the session expire.
但是,是的,我建议只是让会议到期。
#4
If I'm not mistaken Javascript should have a function named something like onWindowClose, maybe try searching for it?
如果我没有弄错,Javascript应该有一个名为onWindowClose的函数,也许可以尝试搜索它?
Regarding PHP solutions, I'm not sure if there are any but I suggest you take a quick look into PHP Connection Handling, specifically the connection_aborted() and register_shutdown_function() functions.
关于PHP解决方案,我不确定是否有,但我建议您快速了解PHP连接处理,特别是connection_aborted()和register_shutdown_function()函数。