Database Reference
In-Depth Information
# construct content of page body
my $page_body =
p ( "This session has been active for $session{count} requests." )
. p ( "The requests occurred at these times:" )
. ol ( li ( $session { timestamp }))
. p ( "Reload page to send next request." );
if ( $session { count } < 10 ) # close (and save) session
{
untie ( %session );
}
else # destroy session after 10 invocations
{
tied ( %session ) -> delete ();
# reset cookie to tell browser to discard session cookie
$cookie = cookie ( - name => "PERLSESSID" ,
- value => $sess_id ,
- expires => "-1d" ); # "expire yesterday"
}
$dbh -> disconnect ();
# generate the output page; include cookie in headers if it's defined
print header ( - cookie => $cookie )
. start_html ( - title => $title )
. $page_body
. end_html ();
For information about CGI.pm cookie support, use the following command and read
the section describing the cookie() function:
% perldoc CGI
To try the script, install it in your web server's cgi-bin directory and request it from your
browser. To reinvoke it, use your browser's Reload function.
sess_track.pl opens the session and increments the counter prior to generating any page
output. This is necessary because the client must be sent a cookie containing the session
name and identifier if the session is new. Any cookie sent must be part of the response
headers, so the page body cannot be sent until after the headers.
The script saves the page content in a variable rather than writing it immediately. Should
the session need to be terminated, the script resets the cookie to be one that tells the
browser to discard the one it has. This must be determined prior to sending any page
content.
Search WWH ::




Custom Search