Challenge - 5 Problems
Session Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Understanding Flask session lifetime default behavior
In Flask, what is the default behavior of the session lifetime if you do not explicitly set
PERMANENT_SESSION_LIFETIME?Attempts:
2 left
💡 Hint
Think about what happens when you close your browser without setting anything.
✗ Incorrect
By default, Flask sessions are not permanent, so they last only until the browser is closed. This means the session cookie is a browser session cookie.
❓ state_output
intermediate2:00remaining
Effect of setting session.permanent to True
Given this Flask code snippet, what will be the lifetime of the session cookie if
PERMANENT_SESSION_LIFETIME is set to 1 day and session.permanent = True is set?from flask import Flask, session
from datetime import timedelta
app = Flask(__name__)
app.secret_key = 'secret'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=1)
@app.route('/')
def index():
session.permanent = True
session['user'] = 'Alice'
return 'Session set'Attempts:
2 left
💡 Hint
Setting
session.permanent = True changes the cookie type.✗ Incorrect
When session.permanent is set to True, Flask uses the PERMANENT_SESSION_LIFETIME config to set the cookie expiration. Here it is 1 day.
🔧 Debug
advanced2:00remaining
Why does session not expire as expected?
A developer sets
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=5) and session.permanent = True in their Flask app. But the session still lasts longer than 5 minutes. What is the most likely reason?Attempts:
2 left
💡 Hint
Think about how session expiration is refreshed in Flask.
✗ Incorrect
Flask refreshes the session expiration time only when the session cookie is updated. If the cookie is not refreshed on each request, the expiration time remains fixed from the first set.
📝 Syntax
advanced2:00remaining
Correct way to set session lifetime in Flask
Which of the following code snippets correctly sets the Flask session lifetime to 2 hours?
Attempts:
2 left
💡 Hint
Check the exact config key Flask uses for session lifetime.
✗ Incorrect
The correct config key is PERMANENT_SESSION_LIFETIME. Other options are invalid or do not exist.
❓ component_behavior
expert3:00remaining
Session lifetime behavior with multiple requests
Consider a Flask app with
PERMANENT_SESSION_LIFETIME set to 10 minutes and session.permanent = True. A user makes a request at 12:00, then another at 12:08, and then at 12:15. Assuming the session cookie is updated on each request, at what time will the session expire if the user stops making requests after 12:15?Attempts:
2 left
💡 Hint
Think about how Flask refreshes the expiration time on each request.
✗ Incorrect
Flask resets the expiration time on each request that updates the session cookie. So the expiration moves forward with each request.