简体   繁体   中英

Flask session cookie not set in Safari

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM