5 # An Object for Maintaining the Values of Variables Across Pages
7 # Copyright 1999-2003 Axis Data
8 # This code is free software that can be used or redistributed under the
9 # terms of Version 2 of the GNU General Public License, as published by the
10 # Free Software Foundation (http://www.fsf.org).
12 # Author: Edward Almasy (almasy@axisdata.com)
14 # Part of the AxisPHP library v1.2.5
15 # For more information see http://www.axisdata.com/AxisPHP/
21 # ---- PUBLIC INTERFACE --------------------------------------------------
25 global $APSession_Shutdown_Sessions;
27 # save database object
30 # construct session variable name
31 $SessionVar =
"APSessionId".md5(
$DB->DBHostName().$DB->DBName());
33 # if session ID available
34 if (isset($_SESSION[$SessionVar]))
36 # look for session ID in database
37 $this->SessionId = $_SESSION[$SessionVar];
38 $DB->Query(
"SELECT * FROM APSessions WHERE SessionId = "
39 .intval($this->SessionId));
41 # if matching session ID record not found in database
42 if (
$DB->NumRowsSelected() < 1)
45 unset($this->SessionId);
50 if (isset($this->SessionId))
52 # load session variables from database
53 $DB->Query(
"SELECT * FROM APSessionData WHERE SessionId = "
54 .intval($this->SessionId));
55 while ($Record =
$DB->FetchRow())
57 $VarName = $Record[
"DataName"];
58 $VarValue = unserialize($Record[
"DataValue"]);
59 if (substr($VarName, -2) ==
"-T")
61 $VarName = substr($VarName, 0, -2);
62 $this->SaveVarFlags[$VarName] = FALSE;
66 $this->SaveVarFlags[$VarName] = TRUE;
67 $this->TempVarFlags[$VarName] = FALSE;
69 $this->SessionVariables[$VarName] = $VarValue;
70 $GLOBALS[$VarName] = $VarValue;
75 # generate unique session ID
78 $this->SessionId = mt_rand();
79 }
while (
$DB->Query(
"SELECT COUNT(*) AS FoundCount FROM APSessionData"
80 .
" WHERE SessionId = ".$this->SessionId,
"FoundCount"));
86 # make sure session state will be saved when page ends
87 $APSession_Shutdown_Sessions[] =& $this;
92 # add variable to list of variables to be saved
95 $this->SessionVariables[$VariableName] = $Value;
99 $this->SessionVariables[$VariableName] = $GLOBALS[$VariableName];
101 $this->SaveVarFlags[$VariableName] = TRUE;
102 $this->TempVarFlags[$VariableName] = FALSE;
107 # add variable to list of variables to be saved
110 $this->SessionVariables[$VariableName] = $Value;
114 if (isset($GLOBALS[$VariableName]))
116 $this->SessionVariables[$VariableName] = $GLOBALS[$VariableName];
120 $this->SessionVariables[$VariableName] = NULL;
123 $this->SaveVarFlags[$VariableName] = TRUE;
124 $this->TempVarFlags[$VariableName] = TRUE;
129 # remove variable from list of variables to be saved (if present)
130 if (isset($this->SessionVariables[$VariableName]))
132 unset($this->SessionVariables[$VariableName]);
133 unset($this->TempVarFlags[$VariableName]);
139 return (isset($this->SessionVariables[$VariableName]) ? TRUE : FALSE);
144 return ((isset($this->SessionVariables[$VariableName]) && $this->TempVarFlags[$VariableName])
148 # retrieve variable with specified name
149 function Get($VariableName)
151 if (isset($this->SessionVariables[$VariableName]))
153 return $this->SessionVariables[$VariableName];
161 # retrieve variable with specified name from all active sessions
164 # clear out any expired sessions
167 # start with empty array
168 $ReturnValue = array();
170 # for each instance of variable in session database
172 $DB->Query(
"SELECT SessionId,DataValue FROM APSessionData WHERE DataName = '".$VariableName.
"'");
173 while ($Record =
$DB->FetchRow())
175 # unpack variable value and add to array to be returned
176 $ReturnValue[$Record[
"SessionId"]] = unserialize($Record[
"DataValue"]);
179 # return array of variable values to caller
184 # ---- PRIVATE INTERFACE -------------------------------------------------
186 # handle to SQL database we use to store session information
192 # array containing variables to be maintained between pages
195 # flags indicating whether to save variable for next session
198 # flags indicating whether to mark variable as temporary for next session
201 # how long before sessions will expire (in minutes)
206 # if session record not found in database
207 $this->DB->Query(
"SELECT * FROM APSessions WHERE SessionId = "
208 .intval($this->SessionId));
209 if ($this->DB->NumRowsSelected() < 1)
211 # create new session record
212 $this->DB->Query(sprintf(
"INSERT INTO APSessions "
213 .
"(SessionId, LastActiveDate) VALUES "
219 # update last active timestamp for session
220 $this->DB->query(
"UPDATE APSessions "
221 .
"SET LastActiveDate=NOW() "
222 .
"WHERE SessionId = ".intval($this->SessionId));
225 # clear all old stored session variables from database
226 $this->DB->Query(sprintf(
"DELETE FROM APSessionData WHERE SessionId = '%d'",
229 # save session variables to database (if any)
230 if (isset($this->SessionVariables))
232 foreach ($this->SessionVariables as $VariableName => $VariableValue)
234 if ($this->SaveVarFlags[$VariableName])
236 if ($this->TempVarFlags[$VariableName]) { $VariableName .=
"-T"; }
237 $this->DB->Query(sprintf(
"INSERT INTO APSessionData "
238 .
"(SessionId, DataName, DataValue) VALUES "
242 addslashes(serialize($VariableValue))));
247 # clear any expired sessions from database
253 # retrieve expired session records
255 $DB->Query(sprintf(
"SELECT * FROM APSessions WHERE DATE_SUB(NOW(), INTERVAL %d MINUTE) > LastActiveDate",
256 $this->SessionExpirationTime));
258 # if expired sessions were found
259 if (
$DB->NumRowsSelected() > 0)
262 while ($Record =
$DB->FetchRow())
265 $Id[$Record[
"SessionId"]] = 1;
268 # for each saved session record ID
271 # delete any stored session data
272 $DB->Query(sprintf(
"DELETE FROM APSessionData WHERE SessionId=%d",
276 # delete expired session records
277 $DB->Query(sprintf(
"DELETE FROM APSessions WHERE DATE_SUB(NOW(), INTERVAL %d MINUTE) > LastActiveDate",
278 $this->SessionExpirationTime));
285 global $APSession_Shutdown_Sessions;
287 # if we have Sessions to shut down
288 if (isset($APSession_Shutdown_Sessions))
290 # call shutdown functions
291 while (list($Key) = each($APSession_Shutdown_Sessions))
293 $SessionObject =& $APSession_Shutdown_Sessions[$Key];
294 $SessionObject->SaveState();
299 register_shutdown_function(
"APSession_Shutdown");