00001 <?PHP 00002 00003 # 00004 # Axis--Session.php 00005 # An Object for Maintaining the Values of Variables Across Pages 00006 # 00007 # Copyright 1999-2003 Axis Data 00008 # This code is free software that can be used or redistributed under the 00009 # terms of Version 2 of the GNU General Public License, as published by the 00010 # Free Software Foundation (http://www.fsf.org). 00011 # 00012 # Author: Edward Almasy (almasy@axisdata.com) 00013 # 00014 # Part of the AxisPHP library v1.2.5 00015 # For more information see http://www.axisdata.com/AxisPHP/ 00016 # 00017 00018 # initialize PHP session support 00019 session_name("AxisPHP"); 00020 session_start(); 00021 00022 class Session { 00023 00024 # ---- PUBLIC INTERFACE -------------------------------------------------- 00025 00026 function Session(&$DB) 00027 { 00028 global $APSession_Shutdown_Sessions; 00029 00030 # save database object 00031 $this->DB =& $DB; 00032 00033 # construct session variable name 00034 $SessionVar = "APSessionId".md5($DB->DBHostName().$DB->DBName()); 00035 00036 # if session ID available 00037 if (isset($_SESSION[$SessionVar])) 00038 { 00039 # look for session ID in database 00040 $this->SessionId = $_SESSION[$SessionVar]; 00041 $DB->Query(sprintf("SELECT * FROM APSessions WHERE SessionId = '%d'", 00042 $this->SessionId)); 00043 00044 # if matching session ID record not found in database 00045 if ($DB->NumRowsSelected() < 1) 00046 { 00047 # clear session ID 00048 unset($this->SessionId); 00049 } 00050 } 00051 00052 # if session ID found 00053 if (isset($this->SessionId)) 00054 { 00055 # load session variables from database 00056 $DB->Query(sprintf("SELECT * FROM APSessionData WHERE SessionId = '%d'", 00057 $this->SessionId)); 00058 while ($Record = $DB->FetchRow()) 00059 { 00060 $VarName = $Record["DataName"]; 00061 $VarValue = unserialize($Record["DataValue"]); 00062 if (substr($VarName, -2) == "-T") 00063 { 00064 $VarName = substr($VarName, 0, -2); 00065 $this->SaveVarFlags[$VarName] = FALSE; 00066 } 00067 else 00068 { 00069 $this->SaveVarFlags[$VarName] = TRUE; 00070 $this->TempVarFlags[$VarName] = FALSE; 00071 } 00072 $this->SessionVariables[$VarName] = $VarValue; 00073 $GLOBALS[$VarName] = $VarValue; 00074 } 00075 } 00076 else 00077 { 00078 # generate session ID (2,147,483,647 is max size of INT in MySQL) 00079 mt_srand((double)microtime() * 1000000); 00080 $this->SessionId = mt_rand(0, 2147483647); 00081 00082 # save session ID 00083 $_SESSION[$SessionVar] = $this->SessionId; 00084 } 00085 00086 # make sure session state will be saved when page ends 00087 $APSession_Shutdown_Sessions[] =& $this; 00088 } 00089 00090 function RegisterVariable($VariableName, $Value = NULL) 00091 { 00092 # add variable to list of variables to be saved 00093 if ($Value != NULL) 00094 { 00095 $this->SessionVariables[$VariableName] = $Value; 00096 } 00097 else 00098 { 00099 $this->SessionVariables[$VariableName] = $GLOBALS[$VariableName]; 00100 } 00101 $this->SaveVarFlags[$VariableName] = TRUE; 00102 $this->TempVarFlags[$VariableName] = FALSE; 00103 } 00104 00105 function PassVariable($VariableName, $Value = NULL) 00106 { 00107 # add variable to list of variables to be saved 00108 if ($Value != NULL) 00109 { 00110 $this->SessionVariables[$VariableName] = $Value; 00111 } 00112 else 00113 { 00114 if (isset($GLOBALS[$VariableName])) 00115 { 00116 $this->SessionVariables[$VariableName] = $GLOBALS[$VariableName]; 00117 } 00118 else 00119 { 00120 $this->SessionVariables[$VariableName] = NULL; 00121 } 00122 } 00123 $this->SaveVarFlags[$VariableName] = TRUE; 00124 $this->TempVarFlags[$VariableName] = TRUE; 00125 } 00126 00127 function UnregisterVariable($VariableName) 00128 { 00129 # remove variable from list of variables to be saved (if present) 00130 if (isset($this->SessionVariables[$VariableName])) 00131 { 00132 unset($this->SessionVariables[$VariableName]); 00133 unset($this->TempVarFlags[$VariableName]); 00134 } 00135 } 00136 00137 function IsRegistered($VariableName) 00138 { 00139 return (isset($this->SessionVariables[$VariableName]) ? TRUE : FALSE); 00140 } 00141 00142 function IsPassed($VariableName) 00143 { 00144 return ((isset($this->SessionVariables[$VariableName]) && $this->TempVarFlags[$VariableName]) 00145 ? TRUE : FALSE); 00146 } 00147 00148 # retrieve variable with specified name 00149 function Get($VariableName) 00150 { 00151 if (isset($this->SessionVariables[$VariableName])) 00152 { 00153 return $this->SessionVariables[$VariableName]; 00154 } 00155 else 00156 { 00157 return NULL; 00158 } 00159 } 00160 00161 # retrieve variable with specified name from all active sessions 00162 function GetFromAllSessions($VariableName) 00163 { 00164 # clear out any expired sessions 00165 $this->DeleteExpiredSessions(); 00166 00167 # start with empty array 00168 $ReturnValue = array(); 00169 00170 # for each instance of variable in session database 00171 $DB =& $this->DB; 00172 $DB->Query("SELECT SessionId,DataValue FROM APSessionData WHERE DataName = '".$VariableName."'"); 00173 while ($Record = $DB->FetchRow()) 00174 { 00175 # unpack variable value and add to array to be returned 00176 $ReturnValue[$Record["SessionId"]] = unserialize($Record["DataValue"]); 00177 } 00178 00179 # return array of variable values to caller 00180 return $ReturnValue; 00181 } 00182 00183 00184 # ---- PRIVATE INTERFACE ------------------------------------------------- 00185 00186 # handle to SQL database we use to store session information 00187 var $DB; 00188 00189 # session ID 00190 var $SessionId; 00191 00192 # array containing variables to be maintained between pages 00193 var $SessionVariables; 00194 00195 # flags indicating whether to save variable for next session 00196 var $SaveVarFlags; 00197 00198 # flags indicating whether to mark variable as temporary for next session 00199 var $TempVarFlags; 00200 00201 # how long before sessions will expire (in minutes) 00202 var $SessionExpirationTime = 180; 00203 00204 function SaveState() 00205 { 00206 # if session record not found in database 00207 $DB =& $this->DB; 00208 $DB->Query(sprintf("SELECT * FROM APSessions WHERE SessionId = '%d'", 00209 $this->SessionId)); 00210 if ($DB->NumRowsSelected() < 1) 00211 { 00212 # create new session record 00213 $DB->Query(sprintf("INSERT INTO APSessions " 00214 ."(SessionId, LastActiveDate) VALUES " 00215 ."(%d, NOW())", 00216 $this->SessionId)); 00217 } 00218 else 00219 { 00220 # update last active timestamp for session 00221 $this->DB->query(sprintf("UPDATE APSessions " 00222 ."SET LastActiveDate=NOW() " 00223 ."WHERE SessionId='%d'", 00224 $this->SessionId)); 00225 } 00226 00227 # clear all old stored session variables from database 00228 $DB->Query(sprintf("DELETE FROM APSessionData WHERE SessionId = '%d'", 00229 $this->SessionId)); 00230 00231 # save session variables to database (if any) 00232 if (isset($this->SessionVariables)) 00233 { 00234 foreach ($this->SessionVariables as $VariableName => $VariableValue) 00235 { 00236 if ($this->SaveVarFlags[$VariableName]) 00237 { 00238 if ($this->TempVarFlags[$VariableName]) { $VariableName .= "-T"; } 00239 $DB->Query(sprintf("INSERT INTO APSessionData " 00240 ."(SessionId, DataName, DataValue) VALUES " 00241 ."(%d, '%s', '%s')", 00242 $this->SessionId, 00243 $VariableName, 00244 addslashes(serialize($VariableValue)))); 00245 } 00246 } 00247 } 00248 00249 # clear any expired sessions from database 00250 $this->DeleteExpiredSessions(); 00251 } 00252 00253 function DeleteExpiredSessions() 00254 { 00255 # retrieve expired session records 00256 $DB =& $this->DB; 00257 $DB->Query(sprintf("SELECT * FROM APSessions WHERE DATE_SUB(NOW(), INTERVAL %d MINUTE) > LastActiveDate", 00258 $this->SessionExpirationTime)); 00259 00260 # if expired sessions were found 00261 if ($DB->NumRowsSelected() > 0) 00262 { 00263 # for each record 00264 while ($Record = $DB->FetchRow()) 00265 { 00266 # save record ID 00267 $Id[$Record["SessionId"]] = 1; 00268 } 00269 00270 # for each saved session record ID 00271 while (list($SessionId) = each($Id)) 00272 { 00273 # delete any stored session data 00274 $DB->Query(sprintf("DELETE FROM APSessionData WHERE SessionId=%d", 00275 $SessionId)); 00276 } 00277 00278 # delete expired session records 00279 $DB->Query(sprintf("DELETE FROM APSessions WHERE DATE_SUB(NOW(), INTERVAL %d MINUTE) > LastActiveDate", 00280 $this->SessionExpirationTime)); 00281 } 00282 } 00283 }; 00284 00285 function APSession_Shutdown() 00286 { 00287 global $APSession_Shutdown_Sessions; 00288 00289 # if we have Sessions to shut down 00290 if (isset($APSession_Shutdown_Sessions)) 00291 { 00292 # call shutdown functions 00293 while (list($Key) = each($APSession_Shutdown_Sessions)) 00294 { 00295 $SessionObject =& $APSession_Shutdown_Sessions[$Key]; 00296 $SessionObject->SaveState(); 00297 } 00298 } 00299 } 00300 00301 register_shutdown_function("APSession_Shutdown"); 00302 00303 00304 ?>