Database Reference
In-Depth Information
Apache::Session represents session information using a hash. It uses Perl's tie mecha‐
nism to map hash operations onto the storage and retrieval methods used by the un‐
derlying storage manager. Thus, to open a session, declare a hash variable and pass it
to tie . The other arguments to tie are the name of the session module, the session ID,
and a hashref containing information about the database to use. There are two ways to
specify the database connection. One method passes a reference to a hash that contains
connection parameters (and the session table name if you do not use the default name
of sessions ):
my %session ;
tie %session ,
"Apache::Session::MySQL" ,
$sess_id ,
{
DataSource => "DBI:mysql:host=localhost;database=cookbook" ,
UserName => "cbuser" ,
Password => "cbpass" ,
LockDataSource => "DBI:mysql:host=localhost;database=cookbook" ,
LockUserName => "cbuser" ,
LockPassword => "cbpass" ,
TableName => "perl_session"
};
In this case, Apache::Session uses the parameters to open its own connection to MySQL,
which it closes when you close or destroy the session.
The other method passes the handle for an already open database connection (repre‐
sented here by $dbh ):
my %session ;
tie %session ,
"Apache::Session::MySQL" ,
$sess_id ,
{
Handle => $dbh ,
LockHandle => $dbh ,
TableName => "perl_session"
};
If you pass a handle to an open connection as just shown, Apache::Session leaves it open
when you close or destroy the session, on the assumption that you're using the handle
for other purposes elsewhere in the script. Close the connection yourself when you're
done with it.
The $sess_id argument to tie is the session identifier. Its value is either undef to begin
a new session, or the ID of an existing session record (a value that matches the id column
in some existing perl_session table row).
After the session has been opened, you can access its contents. For example, you'll want
to determine its identifier so that you can send it to the client:
Search WWH ::




Custom Search