|
|||||||
1. Introduction
2. Installing Karrigell 3. The Web server 4. Working with Apache or Xitami 5. Programming 6. Debugging 7. Python scripts 8. Karrigell Services 9. Python Inside HTML 10. HTML Inside Python 11. HTMLTags - generate HTML in Python 12. Including documents 13. Sessions 13.1 Example 14. Authentication 15. Internationalization |
13. SessionsSessions 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 :
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 :
From another script you'll have access to this value by :
Session objects support a 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 ExampleIn an HTML file create a form and send it to a PIH script :
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 :
The next script is called without any query string, but it will retrieve the information through the session object :
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 |