Database Reference
In-Depth Information
page
<<
cgi
.
p
{
"Reload page to send next request."
}
if
count
<
10
# save modified values into session
# convert session variables back to strings before saving
session
[
"count"
]
=
count
.
to_s
session
[
"timestamp"
]
=
timestamp
.
join
(
","
)
session
.
close
()
else
# destroy session after 10 invocations
session
.
delete
()
end
dbh
.
disconnect
# generate the output page
cgi
.
out
{
cgi
.
html
{
cgi
.
head
{
cgi
.
title
{
title
} }
+
cgi
.
body
() {
page
}
}
}
CGI::Session
makes no provision for expiring sessions, but you can discard old session
records using a technique similar to that discussed in
Recipe 21.1
. Should you do this,
index the
update_time
column to make
DELETE
statements faster:
ALTER
TABLE
ruby_session
ADD
INDEX
(
update_time
);
21.3. Using MySQL-Based Storage with the PHP
Session Manager
Problem
You want to use session storage for PHP scripts.
Solution
PHP includes session management. By default, it uses temporary files for backing store,
but you can configure it to use MySQL instead.
Discussion
This section shows how to use the PHP native session manager and how to extend it by
implementing a storage module that saves session data in MySQL. If your PHP config‐
uration has the
track_vars
configuration variable enabled (which it is by default), ses‐
sion variables are available as elements of the
$_SESSION
superglobal array. If the
reg
ister_globals
configuration variable is enabled as well, session variables also exist in
your script as global variables of the same names. This is less secure, so this variable is