Database Reference
In-Depth Information
An open session can be terminated rather than closed. Doing so removes the correā
sponding row from the
perl_session
table so that it can no longer be used:
tied
(
%session
)
->
delete
();
A sample Apache::Session application
The following script,
sess_track.pl
, is a short but complete session application. It uses
Apache::Session to track the number of requests in the session and the time of each
request, updating and displaying the information each time it is invoked.
sess_track.pl
uses the CGI.pm cookie management interface to pass the session ID in a cookie named
PERLSESSID
:
#!/usr/bin/perl
# sess_track.pl: session request counting/timestamping demonstration
use
strict
;
use
warnings
;
use
CGI
qw(:standard)
;
use
Cookbook
;
use
Apache::Session::
MySQL
;
my
$title
=
"Perl Session Tracker"
;
my
$dbh
=
Cookbook::
connect
();
# connection to MySQL
my
$sess_id
=
cookie
(
"PERLSESSID"
);
# session ID (undef if new session)
my
%session
;
# session hash
my
$cookie
;
# cookie to send to client
# open the session
tie
%session
,
"Apache::Session::MySQL"
,
$sess_id
,
{
Handle
=>
$dbh
,
LockHandle
=>
$dbh
,
TableName
=>
"perl_session"
};
if
(
!
defined
(
$sess_id
))
# this is a new session
{
# get new session ID, initialize session data, create cookie for client
$sess_id
=
$session
{
_session_id
};
$session
{
count
}
=
0
;
# initialize counter
$session
{
timestamp
}
=
[]
;
# initialize timestamp array
$cookie
=
cookie
(
-
name
=>
"PERLSESSID"
,
-
value
=>
$sess_id
);
}
# increment counter and add current timestamp to timestamp array
++
$session
{
count
};
push
(
@
{
$session
{
timestamp
}},
scalar
(
localtime
(
time
())));