pythonhtmlflasksessionflask-session

How do I make a new session in Flask (session.new)


I saw the description of session.new in the Documents

It said: session.new is True if the session is new, False otherwise.

So I tried two things in the following code:

  1. session.clear() before requset
  2. set permanent_session_lifetime to 1 microsecond

code:

from flask import Flask, render_template, session
from datetime import datetime, timedelta

app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
app.config['permanent_session_lifetime']=timedelta(microseconds=1)

@app.before_request
def clear_session():
    session.permanent=True
    session.clear()

@app.route('/session', methods=['GET','POST'])
def get_session():
    print(session)
    print(session.new)
    return render_template('session.html')

the results are:

<SecureCookieSession {}>
False

Now I have a few questions:


Solution

  • If you look at the source code of session.new, you will notice that it contains,

    new = False
    

    So, by default the value of new is always false. The only you can try is maybe by changing your app's secret key and launching the server fresh where all previous sessions are revoked obviously.