Listing 7: Code That Creates an ASP Session Manager
<%
Option Explicit
Dim guid, cn, cmd, rs, ary, newguid, uip, uagent
guid = request.QueryString()
response.write "Old GUID = " & guid & "
"
uip = Request.ServerVariables("REMOTE_ADDR")
uagent = Request.ServerVariables("HTTP_USER_AGENT")
' Open a connection to SQL Server.
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open "PROVIDER=SQLOLEDB;DATA
SOURCE=SERVERNAME;UID=user;PWD=password;DATABASE=TestDB "
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = cn
' Generate a new GUID.
gen_new_guid 'generatae a new GUID
If guid = "" Then
' Add the GUID to the Sessvars table and initialize its value.
ap_guid
guid = newguid
Else
' If the GUID exists, replace it. Look up existing session variables.
gen_new_guid()
get_sessvars
End if
Function gen_new_guid()
cmd.CommandText = "guidgen"
Set rs = cmd.Execute
If RS.BOF or RS.EOF Then
Response.Write "no value generated"
Else
newguid = rs(0)
Response.Write "New GUID = " & newguid & "
"
Set rs = nothing
End If
End Function
Function ap_guid()
cmd.CommandText = "sessvars_ap_guid " & "'" & newguid _
& "', " &_ "'" & uip & "', " & "'" & uagent & "'"
Set rs = cmd.Execute
Set rs = Nothing
End Function
Function get_sessvars()
cmd.CommandText = "get_sessvars " & "'" & newguid &"'"
Set rs = cmd.Execute
If RS.BOF or RS.EOF Then
Response.Write "No session variables for this GUID" & "
"
Else
ary = rs.GetRows
uip = ary(2,0)
uagent = ary(3,0)
Response.Write "Your IP address is " & uip & "
"
Response.Write "Your browser is " & uagent & "
"
End If
End Function
Function upd_guid()
cmd.CommandText = "sessvars_upd_guid " & "'" _
& guid & "', " & "'" & newguid & "'"
Set rs = cmd.Execute
Set rs = Nothing
End Function
cn.close
%>