blob: 6fcaa509b10543b960ffa9a4a50942241df35d59 [file] [log] [blame]
# cookie example - store context in a cookie
#
# this example demonstrates how to use cookies with websh3
#
# create session context manager with the name "state"
web::cookiecontext state
# produce the page of this sample application
proc page {} {
# get current access counter from state ...
set cnt [state::cget cnt 0]
# ... and increment right away
#
# Note: cookie must be sent before the first "web::put" is used
# This is because a cookie must be sent as a HTTP header
set newcnt $cnt
incr newcnt
state::cset cnt $newcnt
# commit the changes on the state
state::commit
# now the html output
web::put "<html><head><title>Cookie Example</title></head>"
web::put "<body bgcolor=\"#FFFFFF\">"
# change welcome text depending on state
if { $cnt == 0 } {
# the first time a warm welcome
web::put "<h1>Welcome</h1>"
web::put "to the wonderful world of Websh"
} else {
# then just a hello
web::put "<h1>Hello</h1>"
web::put "looks like this is your visit Nr $cnt"
}
web::put "<p>"
# and give the user the change to:
# - invalidate the cookie
web::put "<a href=\"[web::cmdurl clearCookie]\">reset</a>"
web::put " | "
# - to move on to the next page of the application
# in this example, just go back to the same page
web::put "<a href=\"[web::cmdurl default]\">next visit</a>"
# properly close html code
web::put "</body></html>"
}
web::command clearCookie {
# get current cookie, if any
state::init mycookie
# ... and invalidate immediately
state::invalidate
# show page
page
}
web::command default {
# get current cookie, if any
state::init mycookie
# show page
page
}
# dispatch
web::dispatch
# cleanup context after request (to prevent session crosstalk)
state::delete