Database Reference
In-Depth Information
$sess_id = $session { _session_id };
Apache::Session reserves for internal use hash element names that begin with an un‐
derscore, such as _session_id . Other than that, you can use choose your own names
for storing session values.
To save a scalar value in the session, store it by value. To access a scalar, read the value
directly. For example, maintain a scalar counter value as follows, where the counter is
initialized if the session is new, and then incremented and retrieved for display:
$session { count } = 0 if ! exists ( $session { count }); # initialize counter
++ $session { count }; # increment counter
print "counter value: $session{count}\n" ; # print value
To save a nonscalar value such as an array or a hash into the session record, store a
reference to it:
$session { my_array } = \ @my_array ;
$session { my_hash } = \ %my_hash ;
In this case, changes made to @my_array or %my_hash before you close the session will
be reflected in the session contents. To save an independent copy of an array or hash in
the session that will not change when you modify the original, create a reference to the
copy like this:
$session { my_array } = [ @my_array ];
$session { my_hash } = { %my_hash };
To retrieve a nonscalar value, dereference the reference stored in the session:
@my_array = @ { $session { my_array }};
%my_hash = % { $session { my_hash }};
To close a session when you're done with it, pass it to untie :
untie ( %session );
When you close a session, Apache::Session saves it to the perl_session table if you've
made changes to it. This also makes the session values inaccessible, so don't close the
session until you're done with it.
Apache::Session notices changes to “top-level” session record val‐
ues, but might not detect a change to a member of a value stored by
reference (such as an array element). If this is a problem, you can force
Apache::Session to save a session when you close it by assigning any
top-level session element a value. The session ID is always present in
the session hash, so the following idiom provides a convenient way
to force session saving:
$session { _session_id } = $session { _session_id };
Search WWH ::




Custom Search