Detecting user logout on browser close in Django

前端 未结 1 1422
小鲜肉
小鲜肉 2021-01-19 04:49

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 ke

相关标签:
1条回答
  • 2021-01-19 05:36

    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.

    0 讨论(0)
提交回复
热议问题