繁体   English   中英

如何检测用户何时关闭 flask 后端而不是前端的浏览器/选项卡(如果不是有效的 javascript)

[英]How to detect when a user Close browser/tab in flask back-end not in front-end (if not valid javascript)

由于我使用 session 在 10 分钟后关闭用户处于非活动状态。 但是现在我想在用户关闭浏览器或选项卡时清除 Session。

from flask import Flask, render_template, request, session, redirect, url_for, flash, Response, Blueprint, jsonify, \
    send_file,copy_current_request_context
import string
import random
import base64
import binascii
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import os

app = Flask(__name__,template_folder='templates',static_folder='static')
secret_key_=os.urandom(24)
app.secret_key = secret_key_
app.config['SESSION_TYPE'] = 'filesystem'
app.debug = True
BLOCK_SIZE = 16



def pad(data):
    length = BLOCK_SIZE - (len(data) % BLOCK_SIZE)
    return data + chr(length)*length

def unpad(data):
    data = data[:-data[-1]]
    return data

def decrypt(encrypted, key):
    BLOCK_SIZE = 16
    encrypted = base64.b64decode(encrypted)
    IV = encrypted[:BLOCK_SIZE]
    aes = AES.new(key[0:16], AES.MODE_CBC, IV)
    return unpad(aes.decrypt(encrypted[BLOCK_SIZE:]))


@app.route('/', methods=["GET", "POST"])
def login():
    if request.method == "OPTIONS":
        return 403
    else:
        return render_template("home.html")


@app.route('/encryp', methods=["GET", "POST"])
def encryp():
    if request.method == "OPTIONS":
        return 403
    else:
        if session.get('page_buffer') is None:
            print('here123')
            key = b"gAAAAABfafxf7_uZ--GzUq5GMBc6h"
            temp_=decrypt(str(request.form["fname"]),key)
            temp_1 = decrypt(str(request.form["lname12"]), key)
            session['page_buffer'] = "Yes"
            session['fname']=temp_
            session['lname12'] = temp_1
            fname=session.get('fname')
            password = session.get('lname12')
            #password = decrypt(str(request.form["lname12"]),key)
            return render_template("Cep.html",Name12=fname,Password12=password)
        else:
            print('here')
            fname = session.get('fname')
            password = session.get('lname12')
            # password = decrypt(str(request.form["lname12"]),key)
            return render_template("Cep.html", Name12=fname, Password12=password)




if __name__ == '__main__':
    app.run()

任何人都可以帮助我如何检测用户关闭浏览器或 flask 中的选项卡吗? 因为用户关闭浏览器/标签后不应查看同一页面。

JavaScript 示例(不工作):

以下两个不起作用,因为当我重定向到另一个页面时,这两个被击中但我不希望我只想监视浏览器关闭。

window.addEventListener('beforeunload', function(event) {
        console.log('I am the 2nd one.');
      });
      window.addEventListener('unload', function(event) {
        console.log('I am the 4th and last one…');
      });

如果你能提出更好的使用 JavaScript 的方法,那我就可以了。

您可以尝试设置一个 cookie,一旦再次访问该路由,使 Flask 检查是否已经存在这样的 cookie。 如果是这样,您就知道用户以前去过那里。 Cookie 的行为在很大程度上由浏览器改变,也可以很容易地被用户操纵。

一种更可靠和更安全的方法是将其保存在数据库中,以防您可以通过 email 或类似的方式识别用户。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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