Karrigell Documentation

Version 2.2.1 13 12 05

Français

13. Sessions

Sessions are a way for the server to keep in memory information related to an individual user while he is browsing from page to page

Suppose you are on a site where you've found two CD's you'd like to order ; then on another page you find a book. After that you'll be asked a few questions about your address, a message to send if it's a present, then they'll want to know about you credit card. After that you'll be presented a page with all the info you've entered so far and asked if you want to confirm you purchase

Without sessions this would be difficult ; you'd have to pass all previous information as hidden form fields, which would be difficult to implement

With sessions it becomes very easy ; the user is identified with a "session identifier" which is sent with every request to the server under the form of a cookie. This identifier matches an object in the server to which attributes can be set or retrieved throughout the user's navigation

With Karrigell, on each page where you want to modify or access session information, begin by creating a session objet by :

sessionObj=Session()

If you're at the beginning of the session, Karrigell will generate a cookie called sessionId and send it back to the web browser. On subsequent requests, the browser will automatically send this cookie and the server will find the associated object

The session object is an ordinary Python object, you can set values to it as attributes :

sessionObj.name="John Doe"

From another script you'll have access to this value by :

name=sessionObj.name

Session objects support a close() method, which causes the session information to be discarded

You don't really have to explicitely close a session ; Karrigell makes sure there are not more than 1000 simultaneaous sessions and erases the oldest ones when the 1000th is reached

13.1 Example

In an HTML file create a form and send it to a PIH script :

<h3>Who are you ?</h3>
<form action="sessionTestBegin.pih">
Name <input name="myname">
<br>First name <input name="firstname">
<br><input type="submit" value="Ok">
</form>

The script will receive the input through QUERY or the form field variables in the namespace, create a session objet and save name and firstname as attributes of this object :

<h3>Begin session</h3>
<%
sessionObj=Session()
sessionObj.name=_myname
sessionObj.firstname=_firstname
print sessionObj.name
%>
<a href="sessionTestFollow.pih">Next...</a>

The next script is called without any query string, but it will retrieve the information through the session object :

<h3>Session test goes on</h3>
<%
session=Session()
print session.firstname
session.close()
%>

Because the script has closed the session, if you go back to the previous page with your browser and try again the Next... link you'll get a nice Python traceback telling you that the session doesn't have a firtsname attribute any more. Modify the script by deleting or commenting the session.close() line and see what happens