Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    India
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Session variables not accessible within a function?

    This is my global.asa
    Code:
    <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.

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)

  • #3
    New to the CF scene
    Join Date
    Jan 2003
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Sep 2002
    Posts
    82
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Code:
    <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;
    Code:
    <%
    	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
    a credit card for a newbie from Amex: The Amex Blue card would like to start developing a credit history.

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Location
    India
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    		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.

  • #6
    Regular Coder
    Join Date
    Nov 2002
    Location
    India
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK the session variables are getting set in my function now. I bungled up.The following code
    Code:
    If Not (IsNull(Session("iTempFiles")) _
    				Or IsEmpty(Session("iTempFiles")) _
    				Or Trim(Session("iTempFiles")) = "") Then
    			Session("iTempFiles") = 0
    		End If
    was supposed to be
    Code:
    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.

  • #7
    Regular Coder
    Join Date
    Nov 2002
    Location
    India
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's the Session_OnStart sub
    Code:
    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:
    Code:
    <%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

  • #8
    New Coder
    Join Date
    Sep 2002
    Posts
    82
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    a credit card for a newbie from Amex: The Amex Blue card would like to start developing a credit history.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •