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 # create database object if none supplied
33 # save database object (if supplied) or create new one
36 # construct session variable name
37 $SessionVar =
"APSessionId".md5(
$DB->DBHostName().$DB->DBName());
39 # if session ID available
40 if (isset($_SESSION[$SessionVar]))
42 # look for session ID in database
43 $this->SessionId = $_SESSION[$SessionVar];
44 $DB->Query(
"SELECT * FROM APSessions WHERE SessionId = "
45 .intval($this->SessionId));
47 # if matching session ID record not found in database
48 if (
$DB->NumRowsSelected() < 1)
51 unset($this->SessionId);
56 if (isset($this->SessionId))
58 # load session variables from database
59 $DB->Query(
"SELECT * FROM APSessionData WHERE SessionId = "
60 .intval($this->SessionId));
61 while ($Record =
$DB->FetchRow())
63 $VarName = $Record[
"DataName"];
64 $VarValue = unserialize($Record[
"DataValue"]);
65 if (substr($VarName, -2) ==
"-T")
67 $VarName = substr($VarName, 0, -2);
68 $this->SaveVarFlags[$VarName] = FALSE;
72 $this->SaveVarFlags[$VarName] = TRUE;
73 $this->TempVarFlags[$VarName] = FALSE;
75 $this->SessionVariables[$VarName] = $VarValue;
76 $GLOBALS[$VarName] = $VarValue;
81 # generate unique session ID
84 $this->SessionId = mt_rand();
85 }
while (
$DB->Query(
"SELECT COUNT(*) AS FoundCount FROM APSessionData"
86 .
" WHERE SessionId = ".$this->SessionId,
"FoundCount"));
92 # make sure session state will be saved when page ends
93 $APSession_Shutdown_Sessions[] =& $this;
98 # add variable to list of variables to be saved
101 $this->SessionVariables[$VariableName] = $Value;
105 $this->SessionVariables[$VariableName] = $GLOBALS[$VariableName];
107 $this->SaveVarFlags[$VariableName] = TRUE;
108 $this->TempVarFlags[$VariableName] = FALSE;
113 # add variable to list of variables to be saved
116 $this->SessionVariables[$VariableName] = $Value;
120 if (isset($GLOBALS[$VariableName]))
122 $this->SessionVariables[$VariableName] = $GLOBALS[$VariableName];
126 $this->SessionVariables[$VariableName] = NULL;
129 $this->SaveVarFlags[$VariableName] = TRUE;
130 $this->TempVarFlags[$VariableName] = TRUE;
135 # remove variable from list of variables to be saved (if present)
136 if (isset($this->SessionVariables[$VariableName]))
138 unset($this->SessionVariables[$VariableName]);
139 unset($this->TempVarFlags[$VariableName]);
145 return (isset($this->SessionVariables[$VariableName]) ? TRUE : FALSE);
150 return ((isset($this->SessionVariables[$VariableName]) && $this->TempVarFlags[$VariableName])
154 # retrieve variable with specified name
155 function Get($VariableName)
157 if (isset($this->SessionVariables[$VariableName]))
159 return $this->SessionVariables[$VariableName];
167 # retrieve variable with specified name from all active sessions
170 # clear out any expired sessions
173 # start with empty array
174 $ReturnValue = array();
176 # for each instance of variable in session database
178 $DB->Query(
"SELECT SessionId,DataValue FROM APSessionData WHERE DataName = '".$VariableName.
"'");
179 while ($Record =
$DB->FetchRow())
181 # unpack variable value and add to array to be returned
182 $ReturnValue[$Record[
"SessionId"]] = unserialize($Record[
"DataValue"]);
185 # return array of variable values to caller
190 # ---- PRIVATE INTERFACE -------------------------------------------------
192 # handle to SQL database we use to store session information
198 # array containing variables to be maintained between pages
201 # flags indicating whether to save variable for next session
204 # flags indicating whether to mark variable as temporary for next session
207 # how long before sessions will expire (in minutes)
212 # if session record not found in database
213 $this->DB->Query(
"SELECT * FROM APSessions WHERE SessionId = "
214 .intval($this->SessionId));
215 if ($this->DB->NumRowsSelected() < 1)
217 # create new session record
218 $this->DB->Query(sprintf(
"INSERT INTO APSessions "
219 .
"(SessionId, LastActiveDate) VALUES "
225 # update last active timestamp for session
226 $this->DB->query(
"UPDATE APSessions "
227 .
"SET LastActiveDate=NOW() "
228 .
"WHERE SessionId = ".intval($this->SessionId));
231 # clear all old stored session variables from database
232 $this->DB->Query(sprintf(
"DELETE FROM APSessionData WHERE SessionId = '%d'",
235 # save session variables to database (if any)
236 if (isset($this->SessionVariables))
238 foreach ($this->SessionVariables as $VariableName => $VariableValue)
240 if ($this->SaveVarFlags[$VariableName])
242 if ($this->TempVarFlags[$VariableName]) { $VariableName .=
"-T"; }
243 $this->DB->Query(sprintf(
"INSERT INTO APSessionData "
244 .
"(SessionId, DataName, DataValue) VALUES "
248 addslashes(serialize($VariableValue))));
253 # clear any expired sessions from database
259 # retrieve expired session records
261 $DB->Query(sprintf(
"SELECT * FROM APSessions WHERE DATE_SUB(NOW(), INTERVAL %d MINUTE) > LastActiveDate",
262 $this->SessionExpirationTime));
264 # if expired sessions were found
265 if ($DB->NumRowsSelected() > 0)
268 while ($Record = $DB->FetchRow())
271 $Id[$Record[
"SessionId"]] = 1;
274 # for each saved session record ID
275 while (list($SessionId) = each($Id))
277 # delete any stored session data
278 $DB->Query(sprintf(
"DELETE FROM APSessionData WHERE SessionId=%d",
282 # delete expired session records
283 $DB->Query(sprintf(
"DELETE FROM APSessions WHERE DATE_SUB(NOW(), INTERVAL %d MINUTE) > LastActiveDate",
284 $this->SessionExpirationTime));
291 global $APSession_Shutdown_Sessions;
293 # if we have Sessions to shut down
294 if (isset($APSession_Shutdown_Sessions))
296 # call shutdown functions
297 while (list($Key) = each($APSession_Shutdown_Sessions))
299 $SessionObject =& $APSession_Shutdown_Sessions[$Key];
300 $SessionObject->SaveState();
305 register_shutdown_function(
"APSession_Shutdown");
SQL database abstraction object with smart query caching.
GetFromAllSessions($VariableName)
UnregisterVariable($VariableName)
PassVariable($VariableName, $Value=NULL)
RegisterVariable($VariableName, $Value=NULL)
IsRegistered($VariableName)