CWIS Developer Documentation
Axis--Session.php
Go to the documentation of this file.
1 <?PHP
2 
3 #
4 # Axis--Session.php
5 # An Object for Maintaining the Values of Variables Across Pages
6 #
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).
11 #
12 # Author: Edward Almasy (almasy@axisdata.com)
13 #
14 # Part of the AxisPHP library v1.2.5
15 # For more information see http://www.axisdata.com/AxisPHP/
16 #
17 
18 
19 class Session {
20 
21  # ---- PUBLIC INTERFACE --------------------------------------------------
22 
23  function Session(&$DB)
24  {
25  global $APSession_Shutdown_Sessions;
26 
27  # save database object
28  $this->DB =& $DB;
29 
30  # construct session variable name
31  $SessionVar = "APSessionId".md5($DB->DBHostName().$DB->DBName());
32 
33  # if session ID available
34  if (isset($_SESSION[$SessionVar]))
35  {
36  # look for session ID in database
37  $this->SessionId = $_SESSION[$SessionVar];
38  $DB->Query("SELECT * FROM APSessions WHERE SessionId = "
39  .intval($this->SessionId));
40 
41  # if matching session ID record not found in database
42  if ($DB->NumRowsSelected() < 1)
43  {
44  # clear session ID
45  unset($this->SessionId);
46  }
47  }
48 
49  # if session ID found
50  if (isset($this->SessionId))
51  {
52  # load session variables from database
53  $DB->Query("SELECT * FROM APSessionData WHERE SessionId = "
54  .intval($this->SessionId));
55  while ($Record = $DB->FetchRow())
56  {
57  $VarName = $Record["DataName"];
58  $VarValue = unserialize($Record["DataValue"]);
59  if (substr($VarName, -2) == "-T")
60  {
61  $VarName = substr($VarName, 0, -2);
62  $this->SaveVarFlags[$VarName] = FALSE;
63  }
64  else
65  {
66  $this->SaveVarFlags[$VarName] = TRUE;
67  $this->TempVarFlags[$VarName] = FALSE;
68  }
69  $this->SessionVariables[$VarName] = $VarValue;
70  $GLOBALS[$VarName] = $VarValue;
71  }
72  }
73  else
74  {
75  # generate unique session ID
76  do
77  {
78  $this->SessionId = mt_rand();
79  } while ($DB->Query("SELECT COUNT(*) AS FoundCount FROM APSessionData"
80  ." WHERE SessionId = ".$this->SessionId, "FoundCount"));
81 
82  # save session ID
83  $_SESSION[$SessionVar] = $this->SessionId;
84  }
85 
86  # make sure session state will be saved when page ends
87  $APSession_Shutdown_Sessions[] =& $this;
88  }
89 
90  function RegisterVariable($VariableName, $Value = NULL)
91  {
92  # add variable to list of variables to be saved
93  if ($Value != NULL)
94  {
95  $this->SessionVariables[$VariableName] = $Value;
96  }
97  else
98  {
99  $this->SessionVariables[$VariableName] = $GLOBALS[$VariableName];
100  }
101  $this->SaveVarFlags[$VariableName] = TRUE;
102  $this->TempVarFlags[$VariableName] = FALSE;
103  }
104 
105  function PassVariable($VariableName, $Value = NULL)
106  {
107  # add variable to list of variables to be saved
108  if ($Value != NULL)
109  {
110  $this->SessionVariables[$VariableName] = $Value;
111  }
112  else
113  {
114  if (isset($GLOBALS[$VariableName]))
115  {
116  $this->SessionVariables[$VariableName] = $GLOBALS[$VariableName];
117  }
118  else
119  {
120  $this->SessionVariables[$VariableName] = NULL;
121  }
122  }
123  $this->SaveVarFlags[$VariableName] = TRUE;
124  $this->TempVarFlags[$VariableName] = TRUE;
125  }
126 
127  function UnregisterVariable($VariableName)
128  {
129  # remove variable from list of variables to be saved (if present)
130  if (isset($this->SessionVariables[$VariableName]))
131  {
132  unset($this->SessionVariables[$VariableName]);
133  unset($this->TempVarFlags[$VariableName]);
134  }
135  }
136 
137  function IsRegistered($VariableName)
138  {
139  return (isset($this->SessionVariables[$VariableName]) ? TRUE : FALSE);
140  }
141 
142  function IsPassed($VariableName)
143  {
144  return ((isset($this->SessionVariables[$VariableName]) && $this->TempVarFlags[$VariableName])
145  ? TRUE : FALSE);
146  }
147 
148  # retrieve variable with specified name
149  function Get($VariableName)
150  {
151  if (isset($this->SessionVariables[$VariableName]))
152  {
153  return $this->SessionVariables[$VariableName];
154  }
155  else
156  {
157  return NULL;
158  }
159  }
160 
161  # retrieve variable with specified name from all active sessions
162  function GetFromAllSessions($VariableName)
163  {
164  # clear out any expired sessions
165  $this->DeleteExpiredSessions();
166 
167  # start with empty array
168  $ReturnValue = array();
169 
170  # for each instance of variable in session database
171  $DB =& $this->DB;
172  $DB->Query("SELECT SessionId,DataValue FROM APSessionData WHERE DataName = '".$VariableName."'");
173  while ($Record = $DB->FetchRow())
174  {
175  # unpack variable value and add to array to be returned
176  $ReturnValue[$Record["SessionId"]] = unserialize($Record["DataValue"]);
177  }
178 
179  # return array of variable values to caller
180  return $ReturnValue;
181  }
182 
183 
184  # ---- PRIVATE INTERFACE -------------------------------------------------
185 
186  # handle to SQL database we use to store session information
187  var $DB;
188 
189  # session ID
191 
192  # array containing variables to be maintained between pages
194 
195  # flags indicating whether to save variable for next session
197 
198  # flags indicating whether to mark variable as temporary for next session
200 
201  # how long before sessions will expire (in minutes)
203 
204  function SaveState()
205  {
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)
210  {
211  # create new session record
212  $this->DB->Query(sprintf("INSERT INTO APSessions "
213  ."(SessionId, LastActiveDate) VALUES "
214  ."(%d, NOW())",
215  $this->SessionId));
216  }
217  else
218  {
219  # update last active timestamp for session
220  $this->DB->query("UPDATE APSessions "
221  ."SET LastActiveDate=NOW() "
222  ."WHERE SessionId = ".intval($this->SessionId));
223  }
224 
225  # clear all old stored session variables from database
226  $this->DB->Query(sprintf("DELETE FROM APSessionData WHERE SessionId = '%d'",
227  $this->SessionId));
228 
229  # save session variables to database (if any)
230  if (isset($this->SessionVariables))
231  {
232  foreach ($this->SessionVariables as $VariableName => $VariableValue)
233  {
234  if ($this->SaveVarFlags[$VariableName])
235  {
236  if ($this->TempVarFlags[$VariableName]) { $VariableName .= "-T"; }
237  $this->DB->Query(sprintf("INSERT INTO APSessionData "
238  ."(SessionId, DataName, DataValue) VALUES "
239  ."(%d, '%s', '%s')",
240  $this->SessionId,
241  $VariableName,
242  addslashes(serialize($VariableValue))));
243  }
244  }
245  }
246 
247  # clear any expired sessions from database
248  $this->DeleteExpiredSessions();
249  }
250 
252  {
253  # retrieve expired session records
254  $DB =& $this->DB;
255  $DB->Query(sprintf("SELECT * FROM APSessions WHERE DATE_SUB(NOW(), INTERVAL %d MINUTE) > LastActiveDate",
256  $this->SessionExpirationTime));
257 
258  # if expired sessions were found
259  if ($DB->NumRowsSelected() > 0)
260  {
261  # for each record
262  while ($Record = $DB->FetchRow())
263  {
264  # save record ID
265  $Id[$Record["SessionId"]] = 1;
266  }
267 
268  # for each saved session record ID
269  while (list($SessionId) = each($Id))
270  {
271  # delete any stored session data
272  $DB->Query(sprintf("DELETE FROM APSessionData WHERE SessionId=%d",
273  $SessionId));
274  }
275 
276  # delete expired session records
277  $DB->Query(sprintf("DELETE FROM APSessions WHERE DATE_SUB(NOW(), INTERVAL %d MINUTE) > LastActiveDate",
278  $this->SessionExpirationTime));
279  }
280  }
281 };
282 
284 {
285  global $APSession_Shutdown_Sessions;
286 
287  # if we have Sessions to shut down
288  if (isset($APSession_Shutdown_Sessions))
289  {
290  # call shutdown functions
291  while (list($Key) = each($APSession_Shutdown_Sessions))
292  {
293  $SessionObject =& $APSession_Shutdown_Sessions[$Key];
294  $SessionObject->SaveState();
295  }
296  }
297 }
298 
299 register_shutdown_function("APSession_Shutdown");
300 
301 
302 ?>