While importing flask, we import modules such as session
etc.
SecureCookieSession
is a kind of dictionary, that can be accessed using session.
Now, I try to clear all the junk variables that I used while trying to build a website.
One of the answers on stackoverflow used a command like session.clear()
for clearing the contents of a session. But such a command gives an error that no such command exists.
Can anyone point out for me how to clear the SecureCookieSession
and how to clear the session every time I shutdown the server or close the website?
To remove a session variable, use the pop() method on the session object and mention the variable to be removed.
Its good practice to time out logged in session after specific time, you can achieve that with Flask-Login. Default session lifetime is 31 days, user need to specify the login refresh view in case of timeout.
Flask – Sessions A session with each client is assigned a Session ID. The Session data is stored on top of cookies and the server signs them cryptographically. For this encryption, a Flask application needs a defined SECRET_KEY.
from flask import session
session.clear()
I use session like this with flask, it does work.
I don't use SecureCookieSession
though, but maybe it can help.
You can also iterate through the session and call session.pop()
for each key in your session. Pop will remove the variable from the session and you don't have to keep updating your secret key.
for key in list(session.keys()):
session.pop(key)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With