# 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 "Cookie Example" web::put "" # change welcome text depending on state if { $cnt == 0 } { # the first time a warm welcome web::put "

Welcome

" web::put "to the wonderful world of Websh" } else { # then just a hello web::put "

Hello

" web::put "looks like this is your visit Nr $cnt" } web::put "

" # and give the user the change to: # - invalidate the cookie web::put "reset" web::put " | " # - to move on to the next page of the application # in this example, just go back to the same page web::put "next visit" # properly close html code web::put "" } 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