blob: 6deaa3ab84a719bfff862c9a6e273c45b0924202 [file] [log] [blame]
<!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 &quot;state&quot;
</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 &quot;web::put&quot; 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">&quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;Cookie Example&lt;/title&gt;&lt;/head&gt;&quot;</FONT></B>
web::put <B><FONT COLOR="#BC8F8F">&quot;&lt;body bgcolor=\&quot;#FFFFFF\&quot;&gt;&quot;</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">&quot;&lt;h1&gt;Welcome&lt;/h1&gt;&quot;</FONT></B>
web::put <B><FONT COLOR="#BC8F8F">&quot;to the wonderful world of Websh&quot;</FONT></B>
} else {
<I><FONT COLOR="#B22222"># then just a hello
</FONT></I> web::put <B><FONT COLOR="#BC8F8F">&quot;&lt;h1&gt;Hello&lt;/h1&gt;&quot;</FONT></B>
web::put <B><FONT COLOR="#BC8F8F">&quot;looks like this is your visit Nr $cnt&quot;</FONT></B>
}
web::put <B><FONT COLOR="#BC8F8F">&quot;&lt;p&gt;&quot;</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">&quot;&lt;a href=\&quot;[web::cmdurl clearCookie]\&quot;&gt;reset&lt;/a&gt;&quot;</FONT></B>
web::put <B><FONT COLOR="#BC8F8F">&quot; | &quot;</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">&quot;&lt;a href=\&quot;[web::cmdurl default]\&quot;&gt;next visit&lt;/a&gt;&quot;</FONT></B>
<I><FONT COLOR="#B22222"># properly close html code
</FONT></I> web::put <B><FONT COLOR="#BC8F8F">&quot;&lt;/body&gt;&lt;/html&gt;&quot;</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>