1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" /> 5 <title>Using Sessions</title> 6 <meta name="generator" 7 content="amaya 8.1a, see http://www.w3.org/Amaya/" /> 8 <link href="styles.css" rel="stylesheet" type="text/css" /> 9 </head> 10 <body> 11 <h1>Using Sessions</h1> 12 <p>Unlike cookies, session information is always stored on the server 13 and is not communicated back to the user. Consequently, sessions have 14 several advantages over cookies, and the uses of sessions may include 15 the storage of...</p> 16 <ul> 17 <li>Sensitive or private information. Although such information can 18 be stored without needing to 19 encrypt it, in many applications you will still want to 20 encrypt such information anyway, no matter where it is stored or how it 21 is communicated.</li> 22 <li>Large amounts of session information can be stored, or at least 23 larger amounts than are typically allowed using cookies.</li> 24 </ul> 25 <div class="WebStack"> 26 <h3>WebStack API - Using Sessions</h3> 27 <p>In WebStack, a session appears as a dictionary to applications and 28 is acquired for a specific user through the <a 29 href="../apidocs/public/WebStack.Generic.Transaction-class.html">transaction</a> 30 object. The following methods are provided in the transaction for 31 accessing and maintenance of session information:</p> 32 <dl> 33 <dt><code>get_session</code></dt> 34 <dd>This method returns the session for the identified user. The <code>create</code> 35 parameter can be set to a true value to create a new session for a user 36 if no session previously existed; otherwise <code>None</code> is 37 returned in such situations.</dd> 38 <dt><code>expire_session</code></dt> 39 <dd>This method causes the session information associated with the 40 identified user to be forgotten. Note that this may not really happen 41 until the user sends another request, and that the <code>get_session</code> 42 method may still return the current session.</dd> 43 </dl> 44 <p>Session objects, which resemble dictionaries, employ plain Python 45 strings as keys in the accessing of information, and as the values 46 loaded and stored inside the session. Unlike cookies, upon setting 47 information within a session, such information is remembered thereafter 48 without any other actions being necessary to make that information 49 permanent or persistent.</p> 50 <dl> 51 </dl> 52 </div> 53 <h2>How and When to Access Sessions</h2> 54 <p>To find out if a session has already been created, ask for a session 55 by specifying that one should not be automatically created:</p> 56 <pre> # In the respond method...<br /> session = trans.get_session(create=0) # session is None if no session already exists</pre> 57 <p>To ensure that a session exists, just ask for a session:</p> 58 <pre> # In the respond method...<br /> session = trans.get_session() # this is the same as using create=1</pre> 59 <p>Session contents may mostly be accessed like dictionaries, so to 60 access the keys within a session the following code could be used:</p> 61 <pre> # In the respond method after obtaining a session...<br /> for key in session.keys():<br /> [Do something with the key - perhaps obtain values.]</pre> 62 <p>To test the presence of a key, and to access values associated with 63 a key, the usual dictionary methods apply:</p> 64 <pre> # In the respond method after obtaining the session...<br /> if session.has_key("my_data"):<br /> my_data = session["my_data"]<br /> [Do something with my_data.]<br /> session["my_data"] = my_data</pre> 65 <p>The session for the user may be removed or "expired" as follows:</p> 66 <pre> # In the respond method...<br /> trans.expire_session()</pre> 67 <p>Note that WebStack automatically knows which session is to be 68 expired since only one such session can exist for the identified user.</p> 69 </body> 70 </html>