Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flask Session API

Flask Session API

Session is used to store data across different requests from the same user. Flask uses signed cookies by default; data is stored on the client but cannot be tampered with.

Import via from flask import session and use it like a dictionary.


The session proxy object

session is a proxy pointing to the SessionMixin instance of the current request. It is only available in the request context.

Attribute / method Description
session[key] = value Set a session value
session.get(key, default) Safely get a session value, returning default when the key does not exist
session.pop(key, default) Remove and return the value of a specified key
session.clear() Clear all session data
"key" in session Check whether a key exists
session.permanent When set to True, the Session persists after the browser closes (using PERMANENT_SESSION_LIFETIME)
session.modified When modifying a mutable value in the session (such as a list append), you must manually set it to True
session.accessed True when the session has been accessed, and Flask automatically adds the Vary: Cookie header
session.new Whether the session is newly created (some implementations do not support accurate detection)

When modifying nested mutable objects in the session (such as lists or dicts), Flask cannot automatically detect the change.

You must manually set session.modified = True, otherwise the change will not be saved to the cookie.


Configuration Item

Configuration key Default value Description
SECRET_KEY None The key for signing the Session; it must be set to use Sessions
SESSION_COOKIE_NAME "session" The name of the session cookie
SESSION_COOKIE_DOMAIN None The Domain attribute of the cookie
SESSION_COOKIE_PATH None The Path attribute of the cookie
SESSION_COOKIE_HTTPONLY True Prevent JavaScript from accessing the Session cookie
SESSION_COOKIE_SECURE False When True, send the cookie only over HTTPS
SESSION_COOKIE_SAMESITE None "Strict", "Lax", or None
PERMANENT_SESSION_LIFETIME timedelta(days=31) The lifetime of a permanent session
SESSION_REFRESH_EACH_REQUEST True Refresh the Session cookie's validity period on every request

Code Example

Example

from

flask

import

Flask

,

session

,

redirect

,

url_for

app

=

Flask

(

name

)

app.

secret_key

=

"your-secret-key"

@

app.

route

(

"/"

)

def

index

(

)

:

   

if

"username"

in

session:

       

return

f

'Logged in as {session["username"]}'

   

return

'You are not logged in'

@

app.

route

(

"/login"

,

methods

=

[

"GET"

,

"POST"

]

)

def

login

(

)

:

   

if

request.

method

==

"POST"

:

        session

[

"username"

]

=

request.

form

[

"username"

]

       

Set a permanent session (persists for 31 days after the browser closes)

        session.

permanent

=

True

       

return

redirect

(

url_for

(

"index"

)

)

   

return

'

'

@

app.

route

(

"/appends"

)

def

append_to_list

(

)

:

   

Note: modifying a nested list requires manually marking modified

   

if

"items"

not

in

session:

        session

[

"items"

]

=

[

]

    session

[

"items"

]

.

append

(

"new_item"

)

    session.

modified

=

True

 

Must be marked manually!

   

return

f

'Items: {session["items"]}'

@

app.

route

(

"/logout"

)

def

logout

(

)

:

    session.

clear

(

)

   

return

redirect

(

url_for

(

"index"

)

)