Figure 1   Sessions Table Structure


Field
Type
Notes
Description
SID
char (32)
Primary Key
The Session ID
DATA
Text

The actual session data
LASTCHANGE
DateTime

Date and time of last access to the data


Figure 2   CGUIDGenerator

 STDMETHODIMP CGUIDGenerator::get_GetGUID(BSTR *pVal)
 {
     AFX_MANAGE_STATE(AfxGetStaticModuleState())
 
     // This function creates a GUID and returns it as a string
 
     GUID    gGuid;
     CString    sGuid;
 
     CoCreateGuid (&gGuid);
     
     sGuid.Format ("%X%X%X%X%X%X%X%X%X%X%X", 
                   gGuid.Data1, 
                   gGuid.Data2, 
                   gGuid.Data3, 
                   gGuid.Data4[0],
                   gGuid.Data4[1],
                   gGuid.Data4[2],
                   gGuid.Data4[3],
                   gGuid.Data4[4],
                   gGuid.Data4[5],
                   gGuid.Data4[6],
                   gGuid.Data4[7]);
 
     *pVal = sGuid.AllocSysString();
 
     return S_OK;
 }

Figure 3   Sample System Objects

Object
Description
SessionElement
The individual element of a session. It exposes two properties: its name and its value.
ElementCollection
A collection of SessionElement objects that represents the individual user session. New elements can be added to it, and existing ones can be retrieved or deleted.
SessionObject
Fundamentally, a collection of Element- Collection objects. It represents a cache of all the sessions available on the current server. In addition, it can be used to retrieve and save sessions to the database.
Session
A wrapper component that is returned by SessionObject whenever the ASP script requests a session. It provides pass- through access to the appropriate instance of ElementCollection, plus the functionality needed to synchronize the data when changes take place during the script's execution.


Figure 4   The System's Stored Procedures


 Create Procedure CreateRetrieveSession
     (
         @SessionID char (32)
     )
 As
     set nocount on
     
     if exists (select SID from SESSIONS where SID = @SessionID)
         begin
         
             /* if record is already in database, update last modified datetime */
         
             update SESSIONS set LASTCHANGE = GETDATE() where SID = @SessionID
         
         end
         
     else
     
         begin
         
             /* Create a new record for this session ID */
             
             insert SESSIONS (SID, DATA) values (@SessionID, NULL)
             
         end
     
         /* Now, return result to caller */
         
         select DATA, LASTCHANGE from SESSIONS where SID = @SessionID
         
     return 
 
 Create Procedure GetLastModifiedDate
     (
         @SessionID char (32)
     )
 As
     select LASTCHANGE from SESSIONS where SID = @SessionID
 
 Create Procedure PurgeOldSessions
 As
     set nocount on
     
     select SID from SESSIONS where DATEDIFF ("mi", LASTCHANGE, GETDATE()) > 19
     
     delete from SESSIONS where DATEDIFF ("mi", LASTCHANGE, GETDATE()) > 19
     
     return 
 
 Create Procedure UpdateSession
     (
         @SessionID char (32),
         @data text
     )
 As
     set nocount on
     
     update SESSIONS set DATA = @data, 
         LASTCHANGE = GETDATE() where SID = @SessionID
     
     select LASTCHANGE from SESSIONS where SID = @SessionID

Figure 5   Sample global.asa

 <OBJECT RUNAT=Server SCOPE=Application ID="Sessions"  PROGID="MIND.SessionObject"></OBJECT>
 
 <SCRIPT LANGUAGE=VBScript RUNAT=Server> 
     sub Application_OnStart
         Sessions.Init ("DSN=LocalServer;uid=sa")
         
         Application ("bUseCookies") = True
     end sub
 </script>


Figure 6   iSession.asp

 <%
     '----------------
     ' Session handling routines
     '
     ' by Marco Tabini (1999)
     '----------------
     
     dim UserID
     dim msSession
     dim bUseCookies
     
     bUseCookies = Application ("bUseCookies")
     
     ' Retrieve this user's ID
     
     if bUseCookies then
         UserID = Request.Cookies ("MINDSESSIONID")
     else
         UserID = Request.QueryString ("MINDSESSIONID")
     end if
     
     if Trim (UserID) = "" or IsNull (UserID) then
         ' No ID, create a new Session
         
         UserID = Sessions.CreateSessionID
     end if
 
     ' Update cookie (if cookies are enabled)
     
     if bUseCookies then
         Response.Cookies ("MINDSESSIONID") = UserID
         Response.Cookies ("MINDSESSIONID").Expires = DateAdd ("d", 1, Now)
     end if
     
     ' Load Session
     
     Set msSession = Sessions.Session (CStr (UserID))
     
     '--------------
     ' Use this function to append the Session ID to a URL
     ' if you do not want to use cookies.
     '--------------
     
     Function SessionURL (URL)
         if not bUseCookies then
             if InStr (URL, "?") > 0 then
                 SessionURL = URL & "&MINDSESSIONID=" & UserID
             else
                 SessionURL = URL & "?MINDSESSIONID=" & UserID
             end if
         end if
     End Function
 %>