I experienced a strange behavior with my session cookie: Running the flask app on my mac, everything works fine and on any browser the cookie is set.
However, if I run it on a windows server, the session cookie is not set on Safari (and iOS) - but still works on any other browsers. How can this happen? Here is an example of a simple app:
import os
import uuid
from flask import Flask, render_template, session
app = Flask(__name__)
SESSION_LIFETIME = 3600
@app.before_request
def before_request():
# create session
if not session.get('uid'):
session.permanent = True
session['uid'] = uuid.uuid4()
@app.route('/', methods=['GET'])
def test():
return render_template('test.html')
if __name__ == "__main__":
app.secret_key = os.urandom(24)
app.permanent_session_lifetime = SESSION_LIFETIME
app.debug = True
app.run(threaded=True,
host="0.0.0.0",
port=int("5000")
)
with an example test.html:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Wubwub</title>
</head>
<body>
Jojo
</body>
</html>
Why does it work on any browser but not on the (important) Safari? And why does the same code work when run on my mac (accessed from both outside and local), but not on windows? All other browsers work with windows (even from outside).
At the top right, tap More More and then Settings. Tap Site settings and then Cookies. Next to “Cookies,” switch the setting on. To allow third-party cookies, check the box next to “Allow third-party cookies.”
In order to store data across multiple requests, Flask utilizes cryptographically-signed cookies (stored on the web browser) to store the data for a session. This cookie is sent with each request to the Flask app on the server-side where it's decoded.
Flask cookies In Flask, set the cookie on the response object. Use the make_response() function to get the response object from the return value of the view function. After that, the cookie is stored using the set_cookie() function of the response object. It is easy to read back cookies.
I had same behaviour that session variables were not working as they want.
So what I did is removed the session use and to make work like as session I used a list with key-value pair
First init the list
list_name = {'key1':'','key2':''}; and so on...
And store the variables as you wish in this list and access it wherever you want by substituting the keys
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