2

Right now, I'm porting an app I wrote from PHP to Flask, and I've already hit a roadblock on day 2; the original app relied heavily on client-side variables, stored in the $_SESSION array. I can't seem to find a way to do the same in Flask, which is really quite frustrating.

I've tried using JavaScript to do the same tasks, but it ends up being much slower and less reliable than PHP's client-side variable storing method.

Where should I start with this in Flask? Any particular extensions I should look in to? What's the best practice Is it even possible?

Thanks in advance!

Joshua Merriman
  • 925
  • 1
  • 8
  • 13

1 Answers1

3

Flask provides a session object. This object is stored on the client side as a signed cookie (I imagine $_SESSION is similar). It requires that Flask.secret_key be set. The object can be modified like a dict.

from flask import session

color = 'blue'
session['usr_fav_color'] = color
Seberius
  • 399
  • 2
  • 6
  • Thanks for that! I can't believe it's in the Quickstart, too... And I read that thing at least three times a day. – Joshua Merriman Nov 15 '13 at 01:12
  • Actually, PHP's $_SESSION is stored server side in a file with the cookie only holding a session id, similar to the [flask-kvsession extension](http://pythonhosted.org/Flask-KVSession/): http://stackoverflow.com/questions/454635/where-are-session-variables-stored – John Cleaver Jan 27 '16 at 21:52