I am doing a simple project, and I want to have a login session. There is a good example here: http://webpython.codepoint.net/cgi_session_class but I can not understand, how do I check if the session is already established?
1 Answer
That class takes care of handling new sessions entirely transparently.
The only way you can detect if a session is new is by storing something in the session and test for that:
if not 'seen' in session.data:
# new session, set a flag
session.data['seen'] = True
because a new session is always empty, but only if you set values will returning sessions not be empty.
9 Comments
Anarion
Thank you, that's what I guessed. Do you know, why do I get "message = "'Session' object has no attribute 'data'"" error?
Martijn Pieters
@Anarion: you need to create an instance of the
Session class, only instances have the data attribute: session = Session(...).Anarion
That's how I'm using it:
sess = session.Session(expires=365*24*60*60, cookie_path='/') and I get an eroor inside of the class, on thins line: elif isinstance(expires, int): => 51 self.data['cookie']['expires'] = expiresAnarion
MAy be this line didn't work?
self.data = shelve.open(session_dir + '/sess_' + sid, writeback=True)Martijn Pieters
Are you sending the
Set-Cookie header at all? Do read and understand the sample code in that page first. |