Flask Sessions Are Being Removed When Visiting Home Page

Question:
I’m adding a item to Flask sessions, but every time I visit the home page (the URL looks like this: https://example.com/ ← ) the sessions get removed. If I visit any page that has a extension (like this: https://example.com/about) then the sessions don’t get removed. What can I do to fix this?

1 Like

You have to ensure the session cookie covers the entire domain, irrespective of the URL path, you can modify the SESSION_COOKIE_PATH configuration in your Flask app. Simply set it as '/' to make the session cookie available across all URL paths.

from flask import Flask, session

app = Flask(__name__)
app.config['SESSION_COOKIE_PATH'] = '/'

Tryed it, but it didn’t work. It’s weird that it only works on all pages but “/“.

@Sky Anything else I can do to fix it?

1 Like

Okay, so I played around with it a bit and it turned out that somehow a session.clear() has snuck itself into my code :open_mouth::person_facepalming:

2 Likes

Awesome, It just magically appeared. :sob:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.