在Django关闭浏览器中检测用户注销 [英] Detecting user logout on browser close in Django

查看:767
本文介绍了在Django关闭浏览器中检测用户注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些数字计算的Web服务。它具有注册模式,其中用户必须注册以通过邮件发送其结果。



我们希望跟踪用户保持登录的时间。成功注册后,登录时间将写入数据库。注册不是永久性的,它只是为了单次会议的目的,用于获取用户电子邮件。



有几种情况可能:



用户通过注销按钮正常注销。
最简单的解决方案。在数据库中写入时间并注销,并删除会话。



用户注销会话到期。
我计划有一个脚本会检查所有没有设置退出时间的数据库条目,如果当前时间 - 登录时间>到期时间在数据库中写入注销时间为登录时间+



用户通过浏览器注销关闭。
会话的get_expire_at_browser_close()设置为True 。但是我不知道服务器如何检测浏览器关闭。



想法,评论家,评论?

解决方案

在django会话中间件中,如果我们想要SESSION_EXPIRE_AT_BROWSER_CLOSE:

 如果settings.SESSION_EXPIRE_AT_BROWSER_CLOSE:
max_age =无
expires =无

根据此页面


通过设置其中之一, cookie将持续到其时间用完为止,否则 - 如果您既不设置,则cookie将持续到关闭浏览器(会话cookie)。


编辑:



O跟踪用户在线时间的方式是通过使用将每次ping服务器的JavaScript来实现。只要用户在浏览器中打开页面,并且每个ping服务器都应该更新用户最后一次看到的值。



当用户关闭浏览器会话结束时。下次用户登录服务器可以计算上次访问的持续时间为上次在线上访 - 上次登录时间



更简单的解决方案,不使用任何javascript:最后一次在线阅读可以使用简单的自定义中间件在每个用户请求上进行更新。


we have a web service for some numerical computing. It has a registered mode, in which a user has to register to have its results sent by mail.

We would like to keep track of how long the user stays logged. The login time is written in the database upon successful registration. Registration in not permanent, it's just for the purpose of single session and is used for acquiring the user email.

There are a few situations possible:

User logs out normally via the logout button. Simplest solution. Write the time and logout in the database, and delete session.

User logs out by session expiry. I'm planning on having a script which would check all the database entries which don't have a set logout time and if current time - login time > expiry time write logout time in a database as login time + expiry time.

User logs out by browser close. The sessions have a get_expire_at_browser_close() set to True. But i don't know how can the server detect browser closure.

Ideas, critics, comments?

解决方案

In django session middleware these lines control session expiration if we want that SESSION_EXPIRE_AT_BROWSER_CLOSE:

if settings.SESSION_EXPIRE_AT_BROWSER_CLOSE:
    max_age = None
    expires = None

Server doesn't have to do detect anything as cookie that has no max_age or expires set should be deleted on the client side, according to this page:

By setting either of these, the cookie will persist until its time runs out, otherwise—if you set neither—the cookie will last until you close your browser (a "session cookie").

Edit:

One way of tracking how long user was online is by using javascript that will ping server every now and then. It will happen only as long as the user has page opened in browser and on every ping server should update last seen online value for the user.

When user closes browser session is over. Next time user logs in server can calculate duration of his last visit as last seen online - last login time.

Simpler solution without using any javascript: last seen online could be updated on every user request using simple custom middleware.

这篇关于在Django关闭浏览器中检测用户注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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