# This example shows how persistent sessions can be stored # on the server. # It shows two HTML forms which have one input field each. # You can switch between these two forms using the submit # buttons. All data given in the form # Define a memory context to hold some configuration variables. # The next statement makes a context called 'config' to hold # these data. It is a cleaner way than making global variables # because it declares them clearly to be configuration vars. web::context config # Now set some configuration variables. Usually put the session # state files in a directory not accessible through the Web server. config::cset stateDirectory /tmp/websh/state/ # Create a file counter that generates the session ids. We take # here an easy number generator, which produces sequential numbers # and stores the actual counter value in a file (only create one if # the current interpreter does not already have one) if {![llength [info commands idGenerator]]} { web::filecounter idGenerator -filename [file join [config::cget stateDirectory] counter] } # Create a file context named 'state'. The option '-path' defines # where the session contexts are stored. '-attachto' defines an # URL parameter name that might contain an existing session. # (This parameter name could in fact be extracted using # 'web::param sid' whenever the session is initalized.) web::filecontext state -path [file join [config::cget stateDirectory] %s] -attachto sid -idgen "idGenerator nextval" # Make sure the session state directory exists catch {file mkdir [config::cget stateDirectory]} proc form {page code} { # Produces a HTML FORM tag. Nested form variables must be output # in 'code'. # The 'page' parameter describes the web::command to call when # the form is submitted. web::put "
[web::htmlify $msg]
" } proc pageOne {{errorMessage ""}} { # Display page one of our HTML form. form processPageOne { if {[string length $errorMessage]} { putErrorMessage $errorMessage } web::put "Numbers only: " web::put "" } } proc pageTwo {{errorMessage ""}} { # Display page two of our HTML form. form processPageTwo { if {[string length $errorMessage]} { putErrorMessage $errorMessage } web::put "Not empty: " web::put "" } } proc saveAllFields {} { # Save all form fields to state context. # web::formvar without parameters returns a list of HTML form # variables sent to this script. web::formvar with the name # of a field returns its value, if the field does not exist # it returns an empty list (or an optional 2nd parameter 'default # value'). # For clarity, we do not handle multiple fields with the same # name correctly here. If a HTML field is given twice or more # 'web::formvar -count