| <!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN"> |
| <HTML> |
| <HEAD> |
| <TITLE>cookie</TITLE> |
| </HEAD> |
| <BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#1F00FF" ALINK="#FF0000" VLINK="#9900DD"> |
| <A NAME="top"> |
| <A NAME="file1"> |
| <H1>cookie.ws3</H1> |
| |
| <PRE> |
| <I><FONT COLOR="#B22222"># cookie example - store context in a cookie |
| </FONT></I><I><FONT COLOR="#B22222"># |
| </FONT></I><I><FONT COLOR="#B22222"># this example demonstrates how to use cookies with websh3 |
| </FONT></I><I><FONT COLOR="#B22222"># |
| </FONT></I> |
| <I><FONT COLOR="#B22222"># create session context manager with the name "state" |
| </FONT></I>web::cookiecontext state |
| |
| <I><FONT COLOR="#B22222"># produce the page of this sample application |
| </FONT></I><B><FONT COLOR="#A020F0">proc</FONT></B> <B><FONT COLOR="#0000FF">page</FONT></B> {<FONT COLOR="#B8860B"></FONT>} { |
| |
| <I><FONT COLOR="#B22222"># get current access counter from state ... |
| </FONT></I> <B><FONT COLOR="#A020F0">set</FONT></B> cnt [state::cget cnt 0] |
| |
| <I><FONT COLOR="#B22222"># ... and increment right away |
| </FONT></I> <I><FONT COLOR="#B22222"># |
| </FONT></I> <I><FONT COLOR="#B22222"># Note: cookie must be sent before the first "web::put" is used |
| </FONT></I> <I><FONT COLOR="#B22222"># This is because a cookie must be sent as a HTTP header |
| </FONT></I> <B><FONT COLOR="#A020F0">set</FONT></B> newcnt $<FONT COLOR="#B8860B">cnt</FONT> |
| <B><FONT COLOR="#A020F0">incr</FONT></B> newcnt |
| state::cset cnt $<FONT COLOR="#B8860B">newcnt</FONT> |
| |
| <I><FONT COLOR="#B22222"># commit the changes on the state |
| </FONT></I> state::commit |
| |
| <I><FONT COLOR="#B22222"># now the html output |
| </FONT></I> web::put <B><FONT COLOR="#BC8F8F">"<html><head><title>Cookie Example</title></head>"</FONT></B> |
| web::put <B><FONT COLOR="#BC8F8F">"<body bgcolor=\"#FFFFFF\">"</FONT></B> |
| |
| <I><FONT COLOR="#B22222"># change welcome text depending on state |
| </FONT></I> <B><FONT COLOR="#A020F0">if</FONT></B> { $<FONT COLOR="#B8860B">cnt</FONT> == 0 } { |
| <I><FONT COLOR="#B22222"># the first time a warm welcome |
| </FONT></I> web::put <B><FONT COLOR="#BC8F8F">"<h1>Welcome</h1>"</FONT></B> |
| web::put <B><FONT COLOR="#BC8F8F">"to the wonderful world of Websh"</FONT></B> |
| } else { |
| <I><FONT COLOR="#B22222"># then just a hello |
| </FONT></I> web::put <B><FONT COLOR="#BC8F8F">"<h1>Hello</h1>"</FONT></B> |
| web::put <B><FONT COLOR="#BC8F8F">"looks like this is your visit Nr $cnt"</FONT></B> |
| } |
| web::put <B><FONT COLOR="#BC8F8F">"<p>"</FONT></B> |
| |
| <I><FONT COLOR="#B22222"># and give the user the change to: |
| </FONT></I> <I><FONT COLOR="#B22222"># - invalidate the cookie |
| </FONT></I> web::put <B><FONT COLOR="#BC8F8F">"<a href=\"[web::cmdurl clearCookie]\">reset</a>"</FONT></B> |
| web::put <B><FONT COLOR="#BC8F8F">" | "</FONT></B> |
| <I><FONT COLOR="#B22222"># - to move on to the next page of the application |
| </FONT></I> <I><FONT COLOR="#B22222"># in this example, just go back to the same page |
| </FONT></I> web::put <B><FONT COLOR="#BC8F8F">"<a href=\"[web::cmdurl default]\">next visit</a>"</FONT></B> |
| |
| <I><FONT COLOR="#B22222"># properly close html code |
| </FONT></I> web::put <B><FONT COLOR="#BC8F8F">"</body></html>"</FONT></B> |
| } |
| |
| |
| web::command clearCookie { |
| |
| <I><FONT COLOR="#B22222"># get current cookie, if any |
| </FONT></I> state::init mycookie |
| |
| <I><FONT COLOR="#B22222"># ... and invalidate immediately |
| </FONT></I> state::invalidate |
| |
| <I><FONT COLOR="#B22222"># show page |
| </FONT></I> page |
| } |
| |
| web::command default { |
| |
| <I><FONT COLOR="#B22222"># get current cookie, if any |
| </FONT></I> state::init mycookie |
| |
| <I><FONT COLOR="#B22222"># show page |
| </FONT></I> page |
| } |
| |
| <I><FONT COLOR="#B22222"># dispatch |
| </FONT></I>web::dispatch |
| |
| <I><FONT COLOR="#B22222"># cleanup context after request (to prevent session crosstalk) |
| </FONT></I>state::delete |
| </PRE> |
| <HR> |
| <ADDRESS>Generated by <A HREF="http://www.iki.fi/~mtr/genscript/">GNU enscript 1.6.3</A>.</ADDRESS> |
| </BODY> |
| </HTML> |