Search:

# CWIS Developers Documentation

- Main Page
- Classes
- Files

- Search for |

- File List
- File Members

# Axis--Session.php

Go to the documentation of this file.

    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("SELECT * FROM APSessions WHERE SessionId = "
    00042                     .intval($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("SELECT * FROM APSessionData WHERE SessionId = "
    00057                     .intval($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 unique session ID
    00079             do
    00080             {
    00081                 $this->SessionId = mt_rand();
    00082             } while ($DB->Query("SELECT COUNT(*) AS FoundCount FROM APSessionData"
    00083                     ." WHERE SessionId = ".$this->SessionId, "FoundCount"));
    00084
    00085             # save session ID
    00086             $_SESSION[$SessionVar] = $this->SessionId;
    00087         }
    00088
    00089         # make sure session state will be saved when page ends
    00090         $APSession_Shutdown_Sessions[] =& $this;
    00091     }
    00092
    00093     function RegisterVariable($VariableName, $Value = NULL)
    00094     {
    00095         # add variable to list of variables to be saved
    00096         if ($Value != NULL)
    00097         {
    00098             $this->SessionVariables[$VariableName] = $Value;
    00099         }
    00100         else
    00101         {
    00102             $this->SessionVariables[$VariableName] = $GLOBALS[$VariableName];
    00103         }
    00104         $this->SaveVarFlags[$VariableName] = TRUE;
    00105         $this->TempVarFlags[$VariableName] = FALSE;
    00106     }
    00107
    00108     function PassVariable($VariableName, $Value = NULL)
    00109     {
    00110         # add variable to list of variables to be saved
    00111         if ($Value != NULL)
    00112         {
    00113             $this->SessionVariables[$VariableName] = $Value;
    00114         }
    00115         else
    00116         {
    00117             if (isset($GLOBALS[$VariableName]))
    00118             {
    00119                 $this->SessionVariables[$VariableName] = $GLOBALS[$VariableName];
    00120             }
    00121             else
    00122             {
    00123                 $this->SessionVariables[$VariableName] = NULL;
    00124             }
    00125         }
    00126         $this->SaveVarFlags[$VariableName] = TRUE;
    00127         $this->TempVarFlags[$VariableName] = TRUE;
    00128     }
    00129
    00130     function UnregisterVariable($VariableName)
    00131     {
    00132         # remove variable from list of variables to be saved (if present)
    00133         if (isset($this->SessionVariables[$VariableName]))
    00134         {
    00135             unset($this->SessionVariables[$VariableName]);
    00136             unset($this->TempVarFlags[$VariableName]);
    00137         }
    00138     }
    00139
    00140     function IsRegistered($VariableName)
    00141     {
    00142         return (isset($this->SessionVariables[$VariableName]) ? TRUE : FALSE);
    00143     }
    00144
    00145     function IsPassed($VariableName)
    00146     {
    00147         return ((isset($this->SessionVariables[$VariableName]) && $this->TempVarFlags[$VariableName])
    00148                 ? TRUE : FALSE);
    00149     }
    00150
    00151     # retrieve variable with specified name
    00152     function Get($VariableName)
    00153     {
    00154         if (isset($this->SessionVariables[$VariableName]))
    00155         {
    00156             return $this->SessionVariables[$VariableName];
    00157         }
    00158         else
    00159         {
    00160             return NULL;
    00161         }
    00162     }
    00163
    00164     # retrieve variable with specified name from all active sessions
    00165     function GetFromAllSessions($VariableName)
    00166     {
    00167         # clear out any expired sessions
    00168         $this->DeleteExpiredSessions();
    00169
    00170         # start with empty array
    00171         $ReturnValue = array();
    00172
    00173         # for each instance of variable in session database
    00174         $DB =& $this->DB;
    00175         $DB->Query("SELECT SessionId,DataValue FROM APSessionData WHERE DataName = '".$VariableName."'");
    00176         while ($Record = $DB->FetchRow())
    00177         {
    00178             # unpack variable value and add to array to be returned
    00179             $ReturnValue[$Record["SessionId"]] = unserialize($Record["DataValue"]);
    00180         }
    00181
    00182         # return array of variable values to caller
    00183         return $ReturnValue;
    00184     }
    00185
    00186
    00187     # ---- PRIVATE INTERFACE -------------------------------------------------
    00188
    00189     # handle to SQL database we use to store session information
    00190     var $DB;
    00191
    00192     # session ID
    00193     var $SessionId;
    00194
    00195     # array containing variables to be maintained between pages
    00196     var $SessionVariables;
    00197
    00198     # flags indicating whether to save variable for next session
    00199     var $SaveVarFlags;
    00200
    00201     # flags indicating whether to mark variable as temporary for next session
    00202     var $TempVarFlags;
    00203
    00204     # how long before sessions will expire (in minutes)
    00205     var $SessionExpirationTime = 180;
    00206
    00207     function SaveState()
    00208     {
    00209         # if session record not found in database
    00210         $this->DB->Query("SELECT * FROM APSessions WHERE SessionId = "
    00211                 .intval($this->SessionId));
    00212         if ($this->DB->NumRowsSelected() < 1)
    00213         {
    00214             # create new session record
    00215             $this->DB->Query(sprintf("INSERT INTO APSessions "
    00216                             ."(SessionId, LastActiveDate) VALUES "
    00217                             ."(%d, NOW())",
    00218                     $this->SessionId));
    00219         }
    00220         else
    00221         {
    00222             # update last active timestamp for session
    00223             $this->DB->query("UPDATE APSessions "
    00224                     ."SET LastActiveDate=NOW() "
    00225                     ."WHERE SessionId = ".intval($this->SessionId));
    00226         }
    00227
    00228         # clear all old stored session variables from database
    00229         $this->DB->Query(sprintf("DELETE FROM APSessionData WHERE SessionId = '%d'",
    00230                 $this->SessionId));
    00231
    00232         # save session variables to database (if any)
    00233         if (isset($this->SessionVariables))
    00234         {
    00235             foreach ($this->SessionVariables as $VariableName => $VariableValue)
    00236             {
    00237                 if ($this->SaveVarFlags[$VariableName])
    00238                 {
    00239                     if ($this->TempVarFlags[$VariableName]) {  $VariableName .= "-T";  }
    00240                     $this->DB->Query(sprintf("INSERT INTO APSessionData "
    00241                                     ."(SessionId, DataName, DataValue) VALUES "
    00242                                     ."(%d, '%s', '%s')",
    00243                             $this->SessionId,
    00244                             $VariableName,
    00245                             addslashes(serialize($VariableValue))));
    00246                 }
    00247             }
    00248         }
    00249
    00250         # clear any expired sessions from database
    00251         $this->DeleteExpiredSessions();
    00252     }
    00253
    00254     function DeleteExpiredSessions()
    00255     {
    00256         # retrieve expired session records
    00257         $DB =& $this->DB;
    00258         $DB->Query(sprintf("SELECT * FROM APSessions WHERE DATE_SUB(NOW(), INTERVAL %d MINUTE) > LastActiveDate",
    00259                 $this->SessionExpirationTime));
    00260
    00261         # if expired sessions were found
    00262         if ($DB->NumRowsSelected() > 0)
    00263         {
    00264             # for each record
    00265             while ($Record = $DB->FetchRow())
    00266             {
    00267                 # save record ID
    00268                 $Id[$Record["SessionId"]] = 1;
    00269             }
    00270
    00271             # for each saved session record ID
    00272             while (list($SessionId) = each($Id))
    00273             {
    00274                 # delete any stored session data
    00275                 $DB->Query(sprintf("DELETE FROM APSessionData WHERE SessionId=%d",
    00276                         $SessionId));
    00277             }
    00278
    00279             # delete expired session records
    00280             $DB->Query(sprintf("DELETE FROM APSessions WHERE DATE_SUB(NOW(), INTERVAL %d MINUTE) > LastActiveDate",
    00281                     $this->SessionExpirationTime));
    00282         }
    00283     }
    00284 };
    00285
    00286 function APSession_Shutdown()
    00287 {
    00288     global $APSession_Shutdown_Sessions;
    00289
    00290     # if we have Sessions to shut down
    00291     if (isset($APSession_Shutdown_Sessions))
    00292     {
    00293         # call shutdown functions
    00294         while (list($Key) = each($APSession_Shutdown_Sessions))
    00295         {
    00296             $SessionObject =& $APSession_Shutdown_Sessions[$Key];
    00297             $SessionObject->SaveState();
    00298         }
    00299     }
    00300 }
    00301
    00302 register_shutdown_function("APSession_Shutdown");
    00303
    00304
    00305 ?>