简体   繁体   English

当用户关闭选项卡时注销

[英]Logout when the user close the tab

On my Django site, I want to logout from the site automatically when I close the tab or browser.在我的 Django 站点上,我想在关闭选项卡或浏览器时自动从站点注销。 I mean when I close the site by closing the tab instead of using logout button, after entering the URL again, I want to see that the user has already logged out from the site to force the user to enter username and password again.我的意思是当我通过关闭选项卡而不是使用注销按钮来关闭站点时,再次输入 URL 后,我想看到用户已经从站点注销以强制用户再次输入用户名和密码。

Is there a way to handle closing the tab in JQuery?有没有办法处理关闭 JQuery 中的选项卡?

Thank you in advance.先感谢您。

Edit: onunload & onbeforeunload events cause to logout the user when also reloading the page.编辑:onunload 和 onbeforeunload 事件导致在重新加载页面时注销用户。

You can use Javascript onunload & onbeforeunload events.您可以使用 Javascript onunloadonbeforeunload事件。 In these events destroy the session cookie for Django.在这些事件中,销毁 Django 的会话 cookie。

Those events are also fired when you leave a site over a link or your browsers back button so be careful and think twice if this is really what you want.当您通过链接或浏览器后退按钮离开网站时,这些事件也会被触发,因此请小心并三思而后行,如果这真的是您想要的。

Add your logout code to the on onunload event.将您的注销代码添加到 on onunload 事件。

window.onunload = function () {
    //logout code here...
}

In JQuery you can use the .unload() function.在 JQuery 中,您可以使用 .unload() 函数。 Remember that you don't have much time so you may send the Ajax request but the result may not reach the client.请记住,您没有太多时间,因此您可以发送 Ajax 请求,但结果可能无法到达客户端。

Another trick is to open a small new window and handle the logout there.另一个技巧是打开一个新的小窗口并在那里处理注销。

window.open("logout url","log out","height=10,width=10,location=no,menubar=no,status=no,titlebar=no,toolbar=no",true);

If you want to disable closing the window (or at least warn the user), you can use this code:如果要禁用关闭窗口(或至少警告用户),可以使用以下代码:

window.onbeforeunload = function(event) {
    //if you return anything but null, it will warn the user.
    //optionally you can return a string which most browsers show to the user as the warning message.
    return true;
}

Another trick is to keep pinging the client every few seconds.另一个技巧是每隔几秒钟就不断地 ping 客户端。 If no reply comes back, assume the user has closed the window, browser has crashed or there is a network issue that ended the chat session anyway.如果没有回复,则假设用户已关闭窗口、浏览器崩溃或存在网络问题导致聊天会话结束。 On the client side, if you don't receive this ping package, you can assume that network connection or server has a problem and you can show the logout warning (and optionally let the user login again).在客户端,如果您没有收到这个 ping 包,您可以假设网络连接或服务器有问题,您可以显示注销警告(并可选择让用户再次登录)。

I just ran into this.我刚遇到这个。 In your Django settings.py file, you can add the flag:在您的 Django settings.py 文件中,您可以添加以下标志:

SESSION_EXPIRE_AT_BROWSER_CLOSE = True SESSION_EXPIRE_AT_BROWSER_CLOSE = 真

This will log the user out when the browser is closed.这将在浏览器关闭时注销用户。 Note that the user will remain logged in if there are other tabs or windows still open (the browser must be completely closed).请注意,如果有其他选项卡或窗口仍然打开(浏览器必须完全关闭) ,用户将保持登录状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM