|
Apache::Session::Counted - Session management via a File::CounterFile |
Apache::Session::Counted - Session management via a File::CounterFile
tie %s, 'Apache::Session::Counted', $sessionid, {
Directory => <root of directory tree>,
DirLevels => <number of dirlevels>,
CounterFile => <filename for File::CounterFile>,
AlwaysSave => <boolean>,
HostID => <string>,
HostURL => <callback>,
Timeout => <seconds>,
}
This session module is based on Apache::Session, but it persues a different notion of a session, so you probably have to adjust your expectations a little.
The dialog that is implemented within an HTTP based application is a nonlinear chain of events. The user can decide to use the back button at any time without informing the application about it. A proper session management must be prepared for this and must maintain the state of every single event. For handling the notion of a session and the notion of a registered user, the application has to differentiate carefully between global state of user data and a user's session related state. Some data may expire after a day, others may be regarded as unexpirable. This module is solely responsible for handling session related data. Saving unexpirable user related data must be handled by the calling application.
In Apache::Session::Counted, a session-ID only lasts from one request to the next at which point a new session-ID is computed by the File::CounterFile module. Thus what you have to treat differently than in Apache::Session are those parts that rely on the session-ID as a fixed token per user. Accordingly, there is no option to delete a session. The remove method is simply disabled as old session data will be overwritten as soon as the counter is reset to zero.
The usage of the module is via a tie as described in the synopsis. The arguments have the following meaning:
sprintf "http://%s/?SESSIONID=%s", <host>, <session-ID>;
The callback can return false to signal that there is no session to retrieve (e.g. when the host or id argument is illegal).
As with other modules in the Apache::Session collection, the tied hash
contains a key _session_id. You must be aware that the value of this
hash entry is not the same as the one you passed in when you retrieved
the session (if you retrieved a session at all). So you have to make
sure that you send your users a new session-id in each response, and
that this is never the old one.
As an implemenation detail it may be of interest to you, that the session ID in Apache::Session::Counted consists of two or three parts: an optional host alias given by the HostID paramter, followed by a colon. Then an ordinary number which is a simple counter which is followed by an underscore. And finally a session-ID like the one in Apache::Session. The number part is used as an identifier of the session and the ID part is used as a password. The number part is easily predictable, but the second part is reasonable unpredictable. We use the first part for implementation details like storage on the disk and the second part to verify the ownership of that token.
Apache::Session::Counted needs Apache::Session and File::CounterFile, all available from the CPAN. The HostID and HostURL parameters for a cluster solution need LWP installed.
The following example resets the counter every 24 hours and keeps the totals of every day as a side effect:
my(@t) = localtime;
tie %session, 'Apache::Session::Counted', $sid,
{
Directory => ...,
DirLevels => ...,
CounterFile => sprintf("/some/dir/%04d-%02d-%02d", $t[5]+1900,$t[4]+1,$t[3])
};
The same effect can be accomplished with a fixed filename and an external cronjob that resets the counter like so:
use File::CounterFile;
$c=File::CounterFile->new("/usr/local/apache/data/perl/sessiondemo/counter");
$c->lock;
$c-- while $c>0;
$c->unlock;
Andreas Koenig <andreas.koenig@anima.de>
This software is copyright(c) 1999-2002 Andreas Koenig. It is free
software and can be used under the same terms as perl, i.e. either the
GNU Public Licence or the Artistic License.
|
Apache::Session::Counted - Session management via a File::CounterFile |