Database Reference
In-Depth Information
if ( $count < 10 ) # save modified values into session
{
$_SESSION [ "count" ] = $count ;
$_SESSION [ "timestamp" ] = $timestamp ;
session_write_close (); # save session changes
}
else # destroy session after 10 invocations
{
session_destroy ();
}
# Produce the output page
?>
<html>
<head><title> <?php print ( $title ); ?> </title></head>
<body>
<?php
print ( "<p>This session has been active for $count requests.</p>" );
print ( "<p>The requests occurred at these times:</p>" );
print make_ordered_list ( $timestamp );
print ( "<p>Reload page to send next request.</p>" );
?>
</body>
</html>
The script includes the Cookbook_Session.php library file to enable the MySQL-based
storage module, then uses the PHP session manager interface in typical fashion. First,
it opens the session and attempts to extract the session variables. For the first request,
the session variables are not set and must be initialized. This is determined by the
isset() tests. The scalar variable $count starts out at zero, and the nonscalar variable
$timestamp starts out as an empty array. For successive requests, the session variables
have the values assigned to them by the previous request.
Next, the script increments the counter, adds the current timestamp to the end of the
timestamp array, and calls session_write_close() to write the changes to session data.
If the session limit of 10 invocations has been reached, the script destroys the session.
This causes the session to restart on the next request.
After updating the session data, sess_track.php produces an output page that displays
the count and the access times.
The output page is produced after updating the session record because PHP might
determine that a cookie containing the session ID must be sent to the client. That de‐
termination must be made before generating the page body because cookies are sent in
the headers.
Search WWH ::




Custom Search