...

View Full Version : Session variables not accessible within a function?



codefox
01-23-2003, 02:39 PM
This is my global.asa

<SCRIPT language="VBScript" runat=Server>
Sub Session_OnStart()
' Store the number of temporary files created during the session.
Session("iTempFiles") = 0
End Sub

Sub Session_OnEnd()
' Delete all temporary files created during the session.
Dim iCount, oFSO
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
For iCount = 0 To Session("iTempFiles") - 1
oFSO.DeleteFile Session("sTempFile" & iCount), True
Next
Set oFSO = Nothing
End Sub
</SCRIPT>
I use a counter variable Session("iTempFiles") to count the number of temporary files created during the session. I also use it as an index to delete the files when the session ends. I have a function in which I use this session variable. Actually I couldn't access any of the session variables within the function. I'm able to access the variables outside the function though. What could be the problem? Aren't session variables available within functions?

Thanks.

raf
01-23-2003, 03:36 PM
I'm not sure at all, but when session on end is executed, does the session-object then still exists for that session ?

I don't think so, but absolutely not sure. Easy to find out: write the values from the session object to a file (or the browser, if still possible) when running on end sub.
(I only used redirects and stuff like that in the onend sub)

nadmji
01-23-2003, 09:09 PM
Session vars should have scope anywhere within your server-side scripts...since you know that it is working outside of your functions, I would just pass Session("iTemp") in as function argument/parameter to work around this issue.

aCcodeMonkey
01-24-2003, 12:05 AM
codefox,

Session onEnd Events are triggered three ways,
[list=1]
Session.Abort() Calls
Session expiration
The browser being closed (least reliable)
[/list=1]

A couple thing in your code.
1. You were not differentiating between session "Temp" files. The global.asa code would have deleted the X files on the directory. Also if your ASP application is not differentiating the files it would overwrite a file that was being used by another user's session. I just added "session.SessionID" to the file names to fix this.

2. You are not passing a valid path to the oFSO object to delete the file(s). you can use request.servervariables("APPL_PHYSICAL_PATH") or Server.MapPath(FileName) to get the root path to the file directory

When you run code from the global.asa file, errors are logged in the events application log. If you have admin privs you can view the loggs via the event viewer.


Once a valid path was set to the files, they were deleted when the session expired. approx 20 minutes after the browser window was closed.

Sample global.asa


<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart
End Sub
Sub Application_OnEnd
End Sub
Sub Session_OnStart
' Store the number of temporary files created during the session.
Session("iTempFiles") = 0
End Sub
Sub Session_OnEnd
' Delete all temporary files created during the session.
Dim iCount, oFSO
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
For iCount = 0 To Abs(Session("iTempFiles")-1)
' Must use "Full" UNC path to file(s)
' Note: Server.MapPath cannot be accessed from Session/Applicaion OnEnd Events in the global.asa
oFSO.DeleteFile "E:\Inetpub\wwwroot\Code_Test\TempSession\temp\TempFile" & session.SessionID & "_" & iCount & ".tmp"
Next
Set oFSO = Nothing
set Session("iTempFiles") = Nothing
End Sub
</SCRIPT>


I used the following code to generatesome dummy tmp files;


<%
Dim iCount, oFSO
Dim sFilePath, sFileName
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
' Write path Info

Response.Write("App Path: " & request.servervariables("APPL_PHYSICAL_PATH") & "<br/>")
Response.Write("Map Path: " & Server.MapPath("CreateFiles.asp") & "<br/>")

sFilePath = Replace(Server.MapPath("CreateFiles.asp"),"CreateFiles.asp","temp\")
For iCount = 0 To 5
sFileName = CStr("TempFile" & session.SessionID & "_" & iCount & ".tmp")
oFSO.CreateTextFile CStr(sFilePath & sFileName),True
Session("iTempFiles") = Session("iTempFiles") + 1
Next
Set oFSO = Nothing

Response.Write(Session("iTempFiles") & " Files Created!")
%>


Hope this helps :cool:

codefox
01-24-2003, 04:45 AM
Thanks for the suggestions. I already use session ID for naming the files and the right files are deleted because I store the exact path with the filename. Here's the actual code where teh temp files are created. This code is within a function where I'm unable to access the session vars:

Dim oFSO, sFileName
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
' Generate a temporary file name using the FSO session object and the
' session ID.
sFileName = oFSO.GetTempName & Session.SessionID & ".tmp"
' Export the chart to a GIF file with the name as the generated file
' name.
oChartSpace.ExportPicture Server.MapPath(sFileName), "png", iWidth, iHeight
' Display this image using the <IMG> tag.
Response.Write "<DIV align=""center""><IMG src=""" & sFileName & """></IMG></DIV>"
' If the session variable counting the number of temporary files is
' not set, set one.
If Not (IsNull(Session("iTempFiles")) _
Or IsEmpty(Session("iTempFiles")) _
Or Trim(Session("iTempFiles")) = "") Then
Session("iTempFiles") = 0
End If
' Create a session variable containing this temporary file name so
' that it could be deleted during the end of session.
Session("sTempFile" & Session("iTempFiles")) = Server.MapPath(sFileName)
' Increment the number of temporary files created during this session.
Session("iTempFiles") = Session("iTempFiles") + 1
' Release the chartspace object
Set oFSO = Nothing

But still haven't figured out why the the session vars are inaccessible within the function. Maybe I should check the session log.

codefox
01-24-2003, 07:42 AM
OK the session variables are getting set in my function now. I bungled up.The following code

If Not (IsNull(Session("iTempFiles")) _
Or IsEmpty(Session("iTempFiles")) _
Or Trim(Session("iTempFiles")) = "") Then
Session("iTempFiles") = 0
End If was supposed to be

If IsNull(Session("iTempFiles")) _
Or IsEmpty(Session("iTempFiles")) _
Or Trim(Session("iTempFiles")) = "" Then
Session("iTempFiles") = 0
End If
Sorry about the confusion, but still I have a problem. The variable Session("iTempFiles") is not set to 0 in global.asa. Couldn't understand why. I tried printing it out in an asp page but it shows nothing.

codefox
01-24-2003, 08:01 AM
Here's the Session_OnStart sub

Sub Session_OnStart()
' Store the number of temporary files created during the session.
Session("iTempFiles") = 1
Session.Timeout = 1
Response.redirect("http://localhost/work/t.asp?" & Session("iTempFiles"))
End Sub


t.asp has the following content:

<%response.write session("iTempFiles")%>
When I open a browser window and type http://locahost, it's automatically redirected to http://localhost/work/t.asp and the query parameter 1 is passed. But the page does not display 1 as it should :confused:

aCcodeMonkey
01-24-2003, 04:53 PM
Since sessions last as long as the browser is open the session objects remain in memory. Even if the person has browsed to another website.

In the session onEnd you need to set the counter bact to 0 or ""
Use: Session("iTempFiles") = 0

In your ASP function you can also override the default by using Session.Expires = 1 "lives for one minute without activity from the web app or browser"


But like I said the onEnd Events can linger until the the default expiration (20 Minutes) set in IIS.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum