You can store all kinds of interesting stuff on an unsuspecting
surfer's machine using
cookies.
You can create a named cookie
object and store a number of values in it. To send it down to the
browser, set a ``cookie'' header in the call to
CGI#out
.
require "cgi"
cookie = CGI::Cookie.new("rubyweb", "CustID=123", "Part=ABC");
cgi = CGI.new("html3")
cgi.out( "cookie" => [cookie] ){
cgi.html{
"\nHTML content here"
}
}
|
produces:
Content-Type: text/html
Content-Length: 86
Set-Cookie: rubyweb=CustID%3D123&Part%3DABC; path=
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML>
HTML content here</HTML>
|
The next time the user comes back to this page, you can retrieve
the cookie values for
CustID
and
Part
, as shown in the
HTML output.
require "cgi"
cgi = CGI.new("html3")
cgi.out{
cgi.html{
cgi.pre{
cookie = cgi.cookies["rubyweb"]
"\nCookies are\n" + cookie.value.join("\n")
}
}
}
|
produces:
Content-Type: text/html
Content-Length: 111
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><PRE>
Cookies are
CustID=123
Part=ABC</PRE></HTML>
|