4

I set SESSION_COOKIE_AGE to some value in my settings.py and I want to retrieve the time that is left before a session dies by using session.get_expiry_age() from a view.

However, it seems the returned value is never changing between different calls (at different times): it stays at SESSION_COOKIE_AGE, meaning the session will never end(?)

Is it a normal behaviour? I would like to notify the user whenever the session is about expire and I don't really see how to do it.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Buddyshot
  • 1,614
  • 1
  • 17
  • 44

2 Answers2

2

Yes, this is the intended behaviour. See this ticket: https://code.djangoproject.com/ticket/18458

You can use get_expiry_date() instead and calculate it from there.

Tom Carrick
  • 6,349
  • 13
  • 54
  • 78
  • Are you sure about `get_expiry_date()`? My tests show the date returned always increases when I call it, such that the time until it expires never decreases, in the same way that `get_expiry_age` never decreases. – run_the_race Oct 21 '20 at 14:42
  • How are you testing it? I assume that something is causing the cookie to be re-set (logging in, middleware, etc.). – Tom Carrick Oct 22 '20 at 15:04
  • Set a `breakpoint()` in the view. set the cookie, then print `request.session['...'].get_expiry_date()`, then print it again. There is nothing happening inbetween the prints as its in breakpoint mode. How are you testing it? – run_the_race Oct 22 '20 at 18:53
  • Looking at the code, which may have changed in the six years since I answered this: you are right, in that the expiry age will usually change as time goes on. But there are some cases where it will return the `SESSION_COOKIE_AGE` value. It's a bit hard to intuit from the code what these cases are. One such case is where the setting is `0` or `None`, but that's not the case for this question. – Tom Carrick Nov 01 '20 at 13:05
0

I tried to find the way to get how much session expiry time is left.

Then, I tested get_expiry_age(), get_session_cookie_age() and get_expiry_date() with "time.sleep(60)" in "test" view as shown below. Then, without counting down(decreasing) session expiry time, get_expiry_age() always returns the value of SESSION_COOKIE_AGE or set_expiry() and get_session_cookie_age() always returns the value of SESSION_COOKIE_AGE and get_expiry_date() returns the session expiry date from the current date as shown below:

from django.shortcuts import render
import time

def test(request):
    print(request.session.get_expiry_age())         # 1209600
    print(request.session.get_session_cookie_age()) # 1209600
    print(request.session.get_expiry_date())        # 2022-08-19 22:19:05.376943+00:00
    
    time.sleep(60)

    print(request.session.get_expiry_age())         # 1209600
    print(request.session.get_session_cookie_age()) # 1209600
    print(request.session.get_expiry_date())        # 2022-08-19 22:20:05.392306+00:00
    
    return render(request, 'test/index.html')

Finally, I couldn't find the way to get how much session expiry time is left.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129