I fear there might be a deep bug
in my mental model of cgi and sessions.
To begin with, I find it difficult to debug Ruby cgi
scripts on my local server(OmniHTTPd under Windows).
If the browser (Opera 6.04) dislikes something, I do
not get any usable diagnostics.
I am using a cgi script that calls itself (method
POST) and I am trying to
be able to switch between past sessions or to create
new sessions, i.e. to maintain state.
Also, I find it hard to know what and when to escape
and/or unescape when I want
to save and recall composite items.
Probably someone has already put together a perfect
example of session management that goes beyond the
example in the Pickaxe book, from which Icould learn.
I’ve been told that the PHP enthusiasts have got it
all solved perfectly, but I hope I need not dig into
that.
There is a shopping cart example in
“CGI Programming with Perl, Chapter 11, Maintaining
State”, which probably answers part of my questions
for Perl CGI scripts, but it looks very bulky.
In Ruby, there ought to be something very simple for a
basic session pattern.
Probably someone has already put together a perfect example of
session management that goes beyond the example in the Pickaxe book,
from which Icould learn.
This is a snippet from some code that I wrote awhile ago to do session
management. I don’t know if it’s “perfect”, but it seems to do exactly
what I want, and is generic in application.
We create a new session if the user doesn’t have one and we just
performed a successful “login”, and store some information (a
timestamp and a username). Otherwise we take the existing session,
assuming it hasn’t expired.
···
require “cgi/session”
[ … further down…]
Die if we don’t have a cookie and we’re not logging in.