Advertisement
Guest User

SessionTransfer.asp

a guest
Mar 15th, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 3.98 KB | None | 0 0
  1. <%@ LANGUAGE="VBScript" %>
  2. <%Option Explicit%>
  3. <!--#include file="Include/adovbs.inc"-->
  4.  
  5. <%
  6.  
  7. '***********************************
  8. 'Functions
  9. '***********************************
  10.  
  11. 'Function returns a valid Globally Unique ID (GUID) for identifying a session.
  12. Function GetGuid()
  13.     Dim TypeLib, guid_temp
  14.    
  15.     Set TypeLib = Server.CreateObject("Scriptlet.TypeLib")
  16.    
  17.     'A true GUID contains a unicode null termination, that needs to be stripped to behave like
  18.     '  a string.
  19.     guid_temp = TypeLib.Guid
  20.     GetGuid = Left(guid_temp, Len(guid_temp) - 2)
  21.        
  22.     Set TypeLib = Nothing
  23. End Function
  24.  
  25.  
  26. 'This function retrieves the Session information identified by the parameter guidIn. The
  27. '  resulting Session information is loaded into the Session object, it is not returned.
  28. Sub GetSessionFromDatabase(guidIn)
  29.     'Declare Variables
  30.     Dim con, cmd, rs, strSql, guidTemp, i, connectionString
  31.  
  32.     'Initialize Variables
  33.     Set con = Server.CreateObject("ADODB.Connection")
  34.     connectionString = Application("Facilities_ConnectionString") & ";WSID=" & Request.ServerVariables("REMOTE_ADDR") & ";"
  35.  
  36. '******************
  37.     con.Open connectionString, Application("Facilities_RuntimeUserName"), Application("Facilities_RuntimePassword") 'CONNECTION STRING HERE
  38. '******************
  39.  
  40.     Set cmd = Server.CreateObject("ADODB.Command")
  41.     cmd.ActiveConnection = con
  42.     i = 1
  43.  
  44.     strSql = "SELECT * FROM ASPSessionState WHERE GUID = '" + guidIn + "'"
  45.     Set rs = Server.CreateObject("ADODB.Recordset")
  46.     rs.Open strSql, con
  47.     Session("sync") = "1"
  48.  
  49.     While Not(rs.EOF)
  50. '       Response.Write("I am most certainly reading session key " + rs("SessionKey") + " with value " + rs("SessionValue") + "    ")
  51.         Session(rs("SessionKey")) = rs("SessionValue")
  52.         rs.MoveNext
  53.     Wend
  54.    
  55.     ' Response.Write ("My session id is " + Session.SessionID + "         ")
  56.  
  57.  
  58.     'Clean up database objects
  59.     rs.Close
  60.     con.Close
  61.  
  62.     Set rs = Nothing
  63.     Set cmd = Nothing
  64.     Set con = Nothing
  65. End Sub
  66.  
  67. 'This performs cleanup of the Session information identified by the parameter guidIn. All Session
  68. '  information in the database with the specified GUID is deleted.
  69. Sub ClearSessionFromDatabase(guidIn)
  70.     'Declare Variables
  71.     Dim con, cmd, strSql, connectionString, guidTemp
  72.     Dim olParams
  73.    
  74.  
  75.     'Initialize Variables
  76.     Set con = Server.CreateObject("ADODB.Connection")
  77.     connectionString = Application("Facilities_ConnectionString") & ";WSID=" & Request.ServerVariables("REMOTE_ADDR") & ";"
  78.  
  79. '******************
  80.     con.Open connectionString, Application("Facilities_RuntimeUserName"), Application("Facilities_RuntimePassword") 'CONNECTION STRING HERE
  81. '******************
  82.  
  83.     'Remove all session variables from the database
  84.     Set cmd = Server.CreateObject("ADODB.Command")
  85.     cmd.ActiveConnection = con
  86.  
  87.     cmd.CommandText = "DELETE FROM ASPSessionState WHERE GUID = ? OR datediff(day, isnull(saved_time, getdate() - 1), getdate()) >= 1"
  88.     cmd.Parameters.Append cmd.CreateParameter("Guid", adVarChar, adParamInput, 254)
  89.  
  90.     'Iterate through all Session variables and add them to the database with the
  91.     '  same GUID as an identifier.
  92.     cmd("Guid")         = GetGuid()
  93.        
  94.     cmd.Execute
  95.  
  96.     'Clean up database objects
  97.     con.Close
  98.     Set cmd = Nothing
  99.     Set con = Nothing
  100. End Sub
  101.  
  102. '***********************************
  103. 'Main code execution
  104. '***********************************
  105.  
  106. Dim guidSave
  107.  
  108. If Request.QueryString("guid") & "" <> "" Then
  109.     guidSave =  Request.QueryString("guid")
  110.     ' Response.Write("Attempting to retrieve " + guidSave + "    ")
  111.     'Retrieve the session information and redirect to the specified URL
  112.     Call GetSessionFromDatabase(Request.QueryString("guid"))
  113.  
  114.     'Clean up the database
  115.     Call ClearSessionFromDatabase(Request.QueryString("guid"))
  116.    
  117.     response.Write ("Done, set cookie")
  118.  
  119. End If
  120. %>
  121.  
  122. <%
  123. ' DEBUG: output, continue...
  124. Function Print(n)    
  125.     Response.Write("<pre>" & n & "</pre>")
  126.     Response.Flush()
  127. End Function
  128.  
  129. ' DEBUG: output, stop!
  130. Function Break(n)    
  131.     Response.Write("<pre>" & n & "</pre>")
  132.     Response.Flush()
  133.     Response.End()
  134. End Function
  135. %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement