The State of State
June 14, 1999
Before launching into pools of Perl, let's consider purpose of
state and why anyone would want to maintain it!
The bookmark example illustrates the simplest form of
state maintenance -- keeping track of, or "remembering",
a single variable. In this case, that variable is the
page number. Note, though, that within a single reading session
you don't typically need to remember the page number
because you simply move forwards sequentially. The last read
page number only matters when you begin a new session.
The bookmark, then, actually illustrates maintaining state
across sessions but not within a session. Shortly we'll
see why
"cookies"
are a common Web technique towards this end.
When surfing the web, a user may visit several pages within
one web site in a short period of time. Or, a user
may visit several pages across several unrelated web sites.
A user may visit two pages on one site, a third page
on an unrelated site, and a fourth page on the first
site again. Consequently, it becomes difficult to nail down
just what type of surfing behavior constitutes a "session"
on the Web. Typically, when we refer to a
session of web surfing, we mean that the user visits
several pages on your site within a relatively short window
of time, and that this user does so with intent -- for
instance, shopping a catalog or filling out a series of
forms.
In contrast, we could say that a visit to one or more pages
on your site followed up with a return visit "awhile"
later -- where "awhile" could mean anything from a
half-hour to 6 months or more -- would constitute
a different session. But as you can see, there is a blurry
line separating the concept of a single session from
multiple sessions; sometimes this line is more specifically
defined, as in minutes, in certain web server configurations
or within certain
CGI
scripts. For now, though, we're concerned
with the "session" as a philosophical
construct.
Unfortunately, like a modern day figure from the Wizard of
Oz, the Web is the beast born without a memory. So,
the fact that you may have selected several options on Page
A of a web site means nothing once you surf to Page
B. D'oh! But, with every web page a universe unto itself,
the options for web development quickly become limited.
How could a user select a sweater to purchase on Page A,
a wristwatch on Page B, and submit the order from Page
C? Faced with this serious shortcoming on the Web, technology
developers have conjured up some techniques which
effectively help web sites to remember information from
page to page, or even from session to session -- the Web
still doesn't have a memory, as you'll see, but these
techniques help it act like it does.
In this article we will see three techniques for maintaining
state on the web. First, the (in)famous "cookies",
will be used to illustrate a manner for remembering a user's
chosen preferences across multiple sessions, or visits
to the site. Our second example will demonstrate the usage
of both the CGI module's own state maintenance feature
as well as the venerable hidden form field. And, of course,
we'll do it all using Perl and the CGI module.
The Perl You Need to Know
The Perl You Need to Know
Cookies: Our Misunderstood Friends
|