To read the article online, visit http://www.4GuysFromRolla.com/webtech/041600-2.3.shtml

A Scalable Alternative to Session Variables, Part 3

By Scott Sargent


In Part 2 we looked at the stored procedures needed for the GUID Session application. In this part, we will examine the needed ASP code, starting with guidsession.asp, a file you'll need to include in all of your ASP pages that use the GUID Session approach.


dim SESSIONID 'put the sessionid into this variable, get it from the QS, 
	   'if the QS isnt available we will retrieve the SessionID from 
	   'the database & put it into this variable
SESSIONID = Request.Querystring("$SESSIONID") 

guidsession.asp contains a function called newsession that obtains a new GUID for a new user. The newsession function that goes to the database & gets a GUID for our user for the very first time. To call this function simply do this sessionid = newsession() Easy enough. Most of the hard work is done by the stored procedure in this one. This function calls the newsession stored procedure.

function newsession()    
    set conn = Server.CreateObject ("ADODB.CONNECTION")
	conn.Open "dsn=xxxxx; uid=xxxxx; pwd=xxx"
	dim usripaddr
	usripaddr = request.servervariables("REMOTE_ADDR")
	MySQL = "Execute newsession '" & usripaddr & "'"
	SET ORS = conn.Execute (MySQL)
	newsession = ORS("SESSIONID")
	ORS.Close
	set conn = nothing
end function

The checkqs function saves our butt if we forget to pass the GUID to some page. This looks for the GUID; if it cannot find it it goes to the database to find it. if this is still unsuccessful it creates a new one & returns it to the calling function. One thing to remember here, is that although this makes it easier as we do not have to remember to pass the GUID, if we do not its an extra database call. This function calls the Retrieve_GUID stored procedure if the GUID isn't in the Querystring

function checkqs(byVal sessionid)
	if sessionid = "" then
       set conn = server.createobject("ADODB.CONNECTION")
		conn.open "DSN=xxxx;UID=xxx;PWD=xxx"

		'the sessionid is gone, we need to retrieve it from the DB
		dim ipAddr 
		ipAddr = Request.Servervariables("REMOTE_ADDR")
		dim mySQL
		mySQL = "Execute retrieve_GUID '" & ipAddr & "'" 
		set ors = conn.execute (mySQL) 
		ID = ors("SESSIONID")
		checkqs = ID
	ors.close
	set conn = nothing
	
	Else
		checkqs = sessionid
	End If
end function

This function is the one we've been waiting for; this is how we store the state information or GUIDSession variables. The most complicated part of this is again with the stored procedure. To call this function all you do is this: input_session id, "USERNAME", "MWITTMAN" if you want to update a value, say set USERNAME to MAPPLEBY, all you do is call this function with the updated data. The stored procedure will check & see if the variable already exists, if it does it will simply update it.

function input_session(byVal Sessionid, byVal Dataname, ByVal Data)
	sessionid = checkqs(sessionid)'verify the session is active 
	set conn = Server.CreateObject ("ADODB.CONNECTION")
	conn.Open "DSN=xxxx;UID=xxx;PWD=xxx"

	sql = "Execute sessiondata '" & Sessionid & "','" & Dataname & "','" & data & "'"
	conn.execute (sql)
	set Conn = nothing
End Function

The retrieve_session function retrieves the information back out of the database; for example, retrieve_session(sessionid, "USERNAME") would return MWITTMAN as per our earlier example. This is the simplest of all our functions.

Function retrieve_session(byVal Sessionid, ByVal Dataname)
	sessionid = checkqs(sessionid)'verify the session is active 
	set conn = Server.createobject ("ADODB.CONNECTION")
	conn.open "DSN=xxxx;UID=xxx;PWD=xxx"
	sql = "Execute retrievesession '" & Sessionid & "','" & Dataname & "'"
	Set ORS = conn.execute (sql)
	retrieve_session = ORS("sessionvaldata")
	ors.close
	set conn = nothing
End Function

Well that's the GUIDSession method, or a Scalable Alternative to Session Variables. Just a few notes about it, for simplicity I have written all of the functions in vbscript & asp, to really get the maximum performance out of this method an MTS Object would work best. In the future I will be working on a VB MTS Component and then perhaps an ATL COM MTS (wish me luck). Another benefit of this method is that you can use this inside of other components or programs. That way you can maintain state across multiple platforms & programming languages.

Also this is a work in progress; if anyone has any suggestions or questions upon how to improve it please feel free to email me. My inspiration for writing this article was that we all could find a better way of maintaining state. I know this isn't a finished program/technique, but I believe it's a very good start.

Happy Programming!

  • Read Part 2
  • Read Part 1


    Attachments:

  • Download the source for guidsession.asp in text format


  • Article Information
    Article Title: A Scalable Alternative to Session Variables, Part 3
    Article Author: Scott Sargent
    Published Date: Sunday, April 16, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/041600-2.3.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers