Playing Hangman with mod_perl
How many different ways are there to write a hangman game? I couldn't
even begin to imagine until I started writing Chapter 5:
Maintaining State with mod_perl. Below are seven
different variants of the classical hangman game, each one of which
maintains state in a different fashion. Try them out to compare
their features and performance!
Feel free to grab the source code, you'll find
the hangman games in the perl/ch5 directory.
This version of the game keeps a
running score of everyone who's used it, and should be the one to use
if you're just looking to play.
- Hidden Fields
- This version of the game maintains its state in hidden fields.
Performance is great and the script is very simple, but it's
ridiculously easy to cheat.
- Cookies
- This version maintains state with a cookie. Performance is
still good and it's a little harder to cheat, but still
possible.
- Untamperable Cookies
- This version adds a message authentication check to the cookie
so that the user can't change its value (to artificially inflate
his score, for example). The user can still peak at the secret
word, however.
- Encrypted Cookies
- This version encrypts the cookie so that it can't be read or
tampered with. Very secure, but performance suffers noticeably
(at least on this underpowered 90 MHz Pentium... donations to
modperl.com gladly accepted).
- Persistent Memory
- This version keeps the state information in a persistent memory
structure maintained by IPC::Shareable. The session ID is kept
in a cookie. To keep memory usage under control, only 100
simultaneous sessions are allowed, with the oldest sessions
deleted as needed.
- Database Sessions, URL Rewriting
- This version stores the state information in a mySQL database,
linked to a session ID. The session ID is appended to the URL
(yuck).
- Database Sessions, Cookies
- This version stores the state information in a mySQL database
and stashes the session ID in a cookie. It also maintains a neat
"top winners" list of everyone who's played.
Lincoln D. Stein, lstein@modperl.com
modperl.com
Last modified: Mon Jul 20 13:46:59 EDT 1998