• subscribe
February 22, 2006 12:00 AM

A Recipe for Replacing Session Variables

Cook up a replacement for session variables by using a cookie, a couple of stored procedures, and a random number
SQL Server Pro
InstantDoc ID #49114
Downloads
49114.zip

Step 3. Write the UUID to a user cookie. Although the UUID is stored in a relatively insecure location outside the database, it doesn't contain any information that would identify the user.The following code writes the UUID to a user cookie and sets an expiration date for the cookie only if the user doesn't have a valid, unexpired cookie from a prior session:

Response.Cookies("SessCookie")= 
     strGuid 
Response.Cookies("SessCookie") 
     .Expires = Date + 1 

For additional cookie security, specify both the domain and path when storing the cookie:

Response.Cookies("SessCookie") 
    .domain = your-domain.com 
Response.Cookies("SessCookie") 
    .path = "/virtual/secure" 

where your-domain.com is the name of the domain that will store the cookie and / virtual/secure is the path to the storage location.

Step 4. Add the session cookie record to the Sessvars table. Listing 2 shows ASP code that calls a stored procedure to add the UUID to the Sessvars table. You'll need to use the connection method appropriate for your environment. You can download the code for the stored procedure at InstantDoc ID 49114.Always use a stored procedure to add even a single record to a table. Stored procedures are precompiled and more secure than dynamic SQL.

Step 5. Read the session cookie on every server page and add, update, or retrieve values from the Sessvars table as necessary. Listing 3 shows the code for reading and sending the cookie data to the stored procedure. At first blush, reading every server page might seem burdensome, but the overhead is low compared with storing session data in memory variables on the Web server. Besides, session variables have a tendency to disappear and are fairly heavy users of server resources. Making the SQL database do the work in the form of precompiled stored procedures frees up Web server resources and is generally more efficient.To improve performance when adding or updating data in your Sessvars table, use a data retrieval method such as GetRows, as the code in Listing 3 does. Using GetRows places record-set data in an array, letting you close the SQL connection immediately.

Step 6. Finally, run an automated SQL job that deletes session data every 24 hours or on a specified schedule.

When you follow this recipe, the Web session will expire only when you want it to, even in clustered or nonheterogeneous environments. If users close the browser and reopen it later,their session data remains intact until the UUID cookie expires or you delete the session data in the SQL database. By keeping the session alive in this manner, you can ensure that otherwise volatile data, such as shopping carts or data searches, persists.

Caveats for Using Cookies
Storing session data in a SQL database and retrieving it with a user cookie has advantages and disadvantages.The main advantage is that you can easily make session data persist without using volatile, resource-intensive memory variables. One disadvantage is that you know only what the user is doing but not who he or she is.And if a different user subsequently visits your site from the same machine, the new user will have the same access as the earlier user on that machine until the session expires.

From a security standpoint, the UUID cookie method shouldn't be considered secure on public Web sites that don't require a login. On a secure intranet or extranet, where users are required to log in, you can still use the cookie-to-SQL session manager, and you'll know who the user is from the context of a secure login.

If your Web site requires users to log in and be validated, you can increase Web page security by storing a second UUID in another user cookie. Link the second UUID to the real user ID in the SQL database behind the scenes using a simple translation table, and expire the second UUID on a regular basis. If someone happens to read the cookies from the user's computer or capture packets from your site, the intruder will see only meaningless, nonsequential numbers. By the time a malicious user discovers the cookie and tries to use it to retrieve data, the cookie likely will have expired and be useless. If your UUIDs conform to published specifications and each one is truly unique and universal, they also foil the game of "guess the next ID" because it's impossible to use one UUID to guess or mathematically derive another UUID. See Related Reading for a list of resources about specifications.

Other Basic Assumptions
The UUID cookie method assumes the user allows cookies. Users who turn them off quickly discover they can't conduct much useful business on the public information highway.You can always check for disabled cookies and redirect the user to another page as appropriate.

The UUID cookie approach also assumes that just one instance of your application is running, because switching between multiple browser sessions using the same cookie doesn't work. The other assumptions are that you've hardened your SQL box, secured it properly behind your demilitarized zone (DMZ), and are using stored procedures instead of dynamic SQL.

This approach for tracking user sessions lets you manage Web sessions on a variety of platforms using any SQL database that supports stored procedures. It also overcomes some of the problems inherent in using session variables. All the ingredients you need to implement this method should be readily available regardless of the environment you're working in.

Related Reading

THE INTERNET ENGINEERING TASK FORCE, NETWORK WORKING GROUP
"A Universally Unique IDentifier (UUID) URN Namespace," July 2005, http://www.ietf.org/rfc/rfc4122.txt

THE OPEN GROUP
"Universal Unique Identifier," 1997, http://www.opengroup.org/onlinepubs/9629399/ apdxa.htm

MICROSOFT HELP AND SUPPORT
"How To Use CoCreateGUID API to Generate a GUID with VB," August 30, 2004 http://support.microsoft.com/kb/176790/en-us



ARTICLE TOOLS

Comments
  • Sean
    4 years ago
    Jan 09, 2008

    Excellent article. I've begun using this on several projects and am glad to be free of Session Variables. In case anyone needs a good routine to create a GUID, here's one that I found.

    Function createGuid()
    Set TypeLib = Server.CreateObject("Scriptlet.TypeLib")
    tg = TypeLib.Guid
    createGuid = left(tg, len(tg)-2)
    createGuid = Replace(createGuid,"{","")
    createGuid = Replace(createGuid,"}","")
    Set TypeLib = Nothing
    End Function

  • Diana
    5 years ago
    Jan 25, 2007

    More on Storing Session State
    I read Susan Perschke's article "A Recipe for Replacing Session Variables" (March 2006, InstantDoc ID 49114), which describes using a back-end SQL Server database to store session state, and I agree that this is an excellent solution. I've been using this technique for a number of years now. The only spin I've put on it is that I save the session information in the form of an XML document. The back-end SQL table has three columns: SessionID (randomly generated), Session XML (the XML document), and SessionExpires, a date/time field indicating when to purge the table row. This date-time column is updated (one day is added) on each visit to the Web server. This aids in maintaining the table size during database maintenance procedures.

    I've created a class library called, oddly enough, SessionState. This library is written in .NET and incorporates, among others, the System.XML namespace and contains functions to Add, Update, Delete elements and attributes; Create, Clear, Delete session objects; and Retrieve and Save the current XML document. It basically wraps the functionality of System.XML into a small set of user-friendly functions for Document Object Model manipulation, and wraps System.Data for persistence.

    Using this technique, I've virtually eliminated having to pass parameters via URL or hidden form fields. The technique has never failed and has simplified our development efforts. It also has opened the door to true load balancing in our Web farm.

    One serendipitous use of this approach is that a customer support person can "jump into" another user's session simply by opening a support Web page and selecting the appropriate session from a list of saved sessions currently in the back-end SQL table. The support person can then reproduce any erroneous behavior that the user may be reporting.

    I knew I couldn't be the only person doing this. It's just that good.
    —Michael Lopez

You must log on before posting a comment.

Are you a new visitor? Register Here