1from flask import Flask, session, redirect, url_for, request
2from markupsafe import escape
3
4app = Flask(__name__)
5
6# Set the secret key to some random bytes. Keep this really secret!
7app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
8
9@app.route('/')
10def index():
11 if 'username' in session:
12 return 'Logged in as %s' % escape(session['username'])
13 return 'You are not logged in'
14
15@app.route('/login', methods=['GET', 'POST'])
16def login():
17 if request.method == 'POST':
18 session['username'] = request.form['username']
19 return redirect(url_for('index'))
20 return '''
21 <form method="post">
22 <p><input type=text name=username>
23 <p><input type=submit value=Login>
24 </form>
25 '''
26
27@app.route('/logout')
28def logout():
29 # remove the username from the session if it's there
30 session.pop('username', None)
31 return redirect(url_for('index'))
32
1from flask import Flask, session
2from flask.ext.session import Session
3
4app = Flask(__name__)
5# Check Configuration section for more details
6SESSION_TYPE = 'redis'
7app.config.from_object(__name__)
8Session(app)
9
10@app.route('/set/')
11def set():
12 session['key'] = 'value'
13 return 'ok'
14
15@app.route('/get/')
16def get():
17 return session.get('key', 'not set')
18
1 @app.route("/login",methods = ["POST","GET"])
2 def login():
3 if request.method == "POST":
4 try:
5 Email = request.form["email"]
6 pwd = request.form["pwd"]
7 with sqlite3.connect("Account.db") as con:
8 cur = con.cursor()
9 print("Connection test")
10 cur.execute("SELECT * FROM Account WHERE Email= ? and Password= ?",(Email, pwd))
11 row = cur.fetchone()
12 print("query test")
13 while row is not None:
14 session['email']=request.form['email']
15 print(row[1])
16 return render_template("success.html",msg = msg)
17 else:
18 msg = "sorry wrong id"
19 return render_template("failure.html",msg = msg)
20 except:
21 con.rollback()
22 msg = "problem"
23if 'email' in session:
24 email = session['email']
25 return render_template("view.html")
26 else:
27 return '<p>Please login first</p>'
1 @app.route("/login",methods = ["POST","GET"])
2 def login():
3 if request.method == "POST":
4 try:
5 Email = request.form["email"]
6 pwd = request.form["pwd"]
7 with sqlite3.connect("Account.db") as con:
8 cur = con.cursor()
9 print("Connection test")
10 cur.execute("SELECT * FROM Account WHERE Email= ? and Password= ?",(Email, pwd))
11 row = cur.fetchone()
12 print("query test")
13 while row is not None:
14 session['email']=request.form['email']
15 print(row[1])
16 return render_template("success.html",msg = msg)
17 else:
18 msg = "sorry wrong id"
19 return render_template("failure.html",msg = msg)
20 except:
21 con.rollback()
22 msg = "problem"
23#In the other function do :
24 if 'email' in session:
25 email = session['email']
26 return render_template("view.html")
27 else:
28 return '<p>Please login first</p>'