<?xml version="1.0" encoding="utf-8"?><!DOCTYPE article  PUBLIC '-//OASIS//DTD DocBook XML V4.4//EN'  'http://www.docbook.org/xml/4.4/docbookx.dtd'><article><articleinfo><title>HelpOnSessions</title></articleinfo><section><title>How sessions work in MoinMoin</title><para>Sessions in <ulink url="http://www.nnx.me/HelpOnSessions/MoinMoin#">MoinMoin</ulink> are implemented using the authentication framework, see <ulink url="http://www.nnx.me/HelpOnSessions/HelpOnAuthentication#">HelpOnAuthentication</ulink> for more details. By default, the function <code>MoinMoin.auth.moin_session</code> is contained in the <code>config.auth</code> list and is responsible for managing sessions. You can use it together with any other authentication method if you need sessions.  </para><para>As a programmer, in order to use session variables, you can use <code>request.session</code> like a dict, values stored there are automatically saved and restored if a session is available. </para><para>Code using the session framework currently includes: </para><itemizedlist><listitem><para>the superuser &quot;change user&quot; functionality, see <ulink url="http://www.nnx.me/HelpOnSessions/HelpOnSuperUser#">HelpOnSuperUser</ulink> </para></listitem><listitem><para>the visited pages trail </para></listitem></itemizedlist><section><title>Anonymous sessions</title><para>Anonymous sessions are supported by including the <code>MoinMoin.auth.moin_anon_session</code> function into <code>config.auth</code> and setting <code>config.anonymous_cookie_lifetime</code>. Cookies for anonymous sessions expire after <code>config.anonymous_cookie_lifetime</code> hours (can be fractional), however, the expiry is not verified. Saved state is removed earliest an hour after the session cookie has expired. </para></section><section><title>Replacing moin_session</title><para>It is possible to replace <code>moin_session</code> in the <code>auth</code> configuration list. The new session handler should assign <code>request.session</code> based on a cookie or other information. The <code>request.session</code> object must be a dict-like object and it should implement session data expiry, cf. <code>MoinMoin.auth.SessionData</code>. </para></section><section><title>Session example code</title><para>Here's an example macro using the session code: </para><programlisting format="linespecific" language="python" linenumbering="numbered" startinglinenumber="1"><lineannotation><![CDATA[# -*- coding: iso-8859-1 -*-]]></lineannotation>

<phrase><![CDATA["""]]></phrase>
<phrase><![CDATA[    Tests session state.]]></phrase>
<phrase><![CDATA["""]]></phrase>

<methodname><![CDATA[Dependencies]]></methodname><![CDATA[ = []]><phrase><![CDATA[']]></phrase><phrase><![CDATA[time]]></phrase><phrase><![CDATA[']]></phrase><![CDATA[]]]>

<token><![CDATA[def]]></token><![CDATA[ ]]><methodname><![CDATA[execute]]></methodname><![CDATA[(]]><methodname><![CDATA[macro]]></methodname><![CDATA[, ]]><methodname><![CDATA[args]]></methodname><![CDATA[):]]>
<![CDATA[    ]]><token><![CDATA[if]]></token><![CDATA[ ]]><phrase><![CDATA[']]></phrase><phrase><![CDATA[test]]></phrase><phrase><![CDATA[']]></phrase><![CDATA[ ]]><token><![CDATA[in]]></token><![CDATA[ ]]><methodname><![CDATA[macro]]></methodname><![CDATA[.]]><methodname><![CDATA[request]]></methodname><![CDATA[.]]><methodname><![CDATA[session]]></methodname><![CDATA[:]]>
<![CDATA[        ]]><token><![CDATA[return]]></token><![CDATA[ ]]><methodname><![CDATA[macro]]></methodname><![CDATA[.]]><methodname><![CDATA[formatter]]></methodname><![CDATA[.]]><methodname><![CDATA[text]]></methodname><![CDATA[(]]><methodname><![CDATA[macro]]></methodname><![CDATA[.]]><methodname><![CDATA[request]]></methodname><![CDATA[.]]><methodname><![CDATA[session]]></methodname><![CDATA[[]]><phrase><![CDATA[']]></phrase><phrase><![CDATA[test]]></phrase><phrase><![CDATA[']]></phrase><![CDATA[])]]>
<![CDATA[    ]]><token><![CDATA[import]]></token><![CDATA[ ]]><methodname><![CDATA[random]]></methodname>
<![CDATA[    ]]><methodname><![CDATA[value]]></methodname><![CDATA[ = ]]><methodname><![CDATA[random]]></methodname><![CDATA[.]]><methodname><![CDATA[randint]]></methodname><![CDATA[(1, 100000)]]>
<![CDATA[    ]]><methodname><![CDATA[macro]]></methodname><![CDATA[.]]><methodname><![CDATA[request]]></methodname><![CDATA[.]]><methodname><![CDATA[session]]></methodname><![CDATA[[]]><phrase><![CDATA[']]></phrase><phrase><![CDATA[test]]></phrase><phrase><![CDATA[']]></phrase><![CDATA[] = ]]><methodname><![CDATA[value]]></methodname>
<![CDATA[    ]]><token><![CDATA[return]]></token><![CDATA[ ]]><methodname><![CDATA[macro]]></methodname><![CDATA[.]]><methodname><![CDATA[formatter]]></methodname><![CDATA[.]]><methodname><![CDATA[text]]></methodname><![CDATA[(]]><phrase><![CDATA["]]></phrase><phrase><![CDATA[set to value ]]></phrase><phrase><![CDATA[%d]]></phrase><phrase><![CDATA["]]></phrase><![CDATA[ % ]]><methodname><![CDATA[value]]></methodname><![CDATA[)]]>
</programlisting></section></section></article>