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