Flask.session在关闭浏览器后仍然存在 [英] Flask.session persisting after close browser
问题描述
我希望能够检测到自上次登录Flask站点以来用户是否已关闭浏览器.我已经读过Flask会话应该在关闭浏览器后到期,但是据我所知,会话数据存储在服务器上,并且在浏览器会话之间持续存在.当用户关闭浏览器时,如何清除所有会话数据?
I want to be able to detect if a user has closed a browser since last logging into a Flask site. I have read that Flask sessions are supposed to expire on closing the browser, but the session data is stored on the server as far as I can tell and is persisting across browser sessions. How can I clear all session data when the user closes the browser?
mainapp.py:
mainapp.py:
@mainapp.route('/')
def home():
if 'user_name' in session:
logger.debug( 'Logged in as {0}'.format(escape(session['user_name'])))
return render_template('home.html')
userviews.py:
userviews.py:
@userviews.route("/login", methods=["GET", "POST"])
def login():
form = LoginForm(request.form)
if form.validate_on_submit():
#get user from db
session['user_name'] = user.user_name
setup.py:
app.secret_key = 'somethingreallysecret'
推荐答案
因为烧瓶会话使用Cookie,即使用户关闭了窗口,数据也会保留下来.您可以通过 Flask-SocketIO 跟踪关闭窗口的使用情况.
Because the flask session uses cookies, the data is persisted even if the user closes the window. You can track use closing the window with Flask-SocketIO.
如果要从会话中删除特定密钥:
If you want to remove a specific key from session:
from flask import session
from flask_socketio import SocketIO, emit
from flask.ext.login import current_user, logout_user
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
@socketio.on('disconnect')
def disconnect_user():
logout_user()
session.pop('yourkey', None)
这篇关于Flask.session在关闭浏览器后仍然存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!