0

Right now I am using Flask and a flask 3rd party library Flask-Session

Using the code below, I reload the page 4 times and get the following output:

set userid[0] 127.0.0.1 - - [27/Sep/2014 22:28:35] "GET / HTTP/1.1" 200 - set userid[1] 127.0.0.1 - - [27/Sep/2014 22:28:37] "GET / HTTP/1.1" 200 - set userid[2] 127.0.0.1 - - [27/Sep/2014 22:28:37] "GET / HTTP/1.1" 200 - set userid[3] 127.0.0.1 - - [27/Sep/2014 22:28:38] "GET / HTTP/1.1" 200 - 

Code:

from flask import Flask, session from flask.ext.session import Session app = Flask(__name__) sess = Session() nextId = 0 def verifySessionId(): global nextId if not 'userId' in session: session['userId'] = nextId nextId += 1 sessionId = session['userId'] print ("set userid[" + str(session['userId']) + "]") else: print ("using already set userid[" + str(session['userId']) + "]") sessionId = session.get('userId', None) return sessionId @app.route("/") def hello(): userId = verifySessionId() return str(userId) if __name__ == "__main__": app.config['SECRET_KEY'] = 'super secret key' app.config['SESSION_TYPE'] = 'filesystem' sess.init_app(app) app.debug = True app.run() 

Shouldn't session['userId] be 'saved out' each time I reload the page?

2
  • 2
    Are cookies enabled? A session id is saved in a cookie, tied to the filesystem storage. Commented Sep 28, 2014 at 2:53
  • @MartijnPieters Yeah... that was the issue. It's been a long few days with this, thanks for your help. Commented Sep 28, 2014 at 2:55

1 Answer 1

2

You need to have cookies enabled for sessions to work. Even Flask-Session cannot track a browser without those.

Flask-Session sets a cookie with a unique id, then later on finds your session data again by that cookie.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.