Using MySQL-Based Sessions in Perl Applications
Problem
You want to use session storage for Perl scripts.
Solution
The Apache::Session
module provides a convenient way to use several different storage
types, including one based on MySQL.
Discussion
Apache::Session is an easy-to-use Perl module
for maintaining state information across multiple web requests.
Despite the name, this module is not dependent on Apache and can be
used in non-web contexts, for example, to maintain persistent state
across multiple invocations of a command-line script. On the other
hand, Apache::Session doesn’t
handle any of the issues associated with tracking the session ID
(sending it to the client in response to the initial request and
extracting it from subsequent requests). The example application
shown here uses cookies to pass the session ID, on the assumption
that the client has cookies enabled.
Installing Apache::Session
If you don’t have
Apache::Session, you can
get it from the CPAN (visit http://cpan.perl.org). Installation is
straightforward, although Apache::Session does
require several other modules that you may need to get first. (When
you install it, Apache::Session should tell you
which required modules you need if any are missing.) After you have
everything installed, create a
table in which to store
session records. The specification for the table comes from the MySQL
storage handler documentation, which you can read using this command:
% perldoc Apache::Session::Store::MySQLThe table can be ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access