XQuery/Basic Session Management

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Motivation[edit | edit source]

You want to associate some behavior of your web application with a users's login session.

Method[edit | edit source]

There are several functions provided by eXist and other web servers to manage information associated with a login session.

xquery version "1.0";

let $session-attributes := session:get-attribute-names()

return
<results>
   {for $session-attribute in $session-attributes
       return
       <session-attribute>{$session-attribute}</session-attribute>
   }
</results>

Before you add any session attributes this might return only a single variable such as:

<results>
   <session-attribute>_eXist_xmldb_user</session-attribute>
</results>


xquery version "1.0";

(: set the group and role :)
let $set-dba-group := session:set-attribute('group', 'dba')
let $set-role-editor := session:set-attribute('role', 'editor')

let $session-attributes := session:get-attribute-names()

return
<results>
   {for $session-attribute in $session-attributes
       return
       <session-attribute>{$session-attribute}</session-attribute>
   }
</results>

This will return the following attributes:

<results>
  <session-attribute>group</session-attribute>
  <session-attribute>role</session-attribute>
  <session-attribute>_eXist_xmldb_user</session-attribute>
</results>

These attributes will remain associated with the user until the user logs out or their session times out, typically after 15 minutes of inactivity.

One sample use of session attributes is to keep track of user interface preferences. For example if a user wants to have their data sorted by a person's zip code you can add that to their session variable.

  let $set-sort := session:set-attribute('sort', 'zip-code')