
Deleting a file via Global.asa when session ends
Thanks for the details, Michael.
I see your point re: the conflict if a user ends one
session and starts another, the previous session should
delete the file when it times out, even though the user
has started a new session. Perhaps I should add the
Session.SessionID variable to the filename also.
However, the problem I am seeing is that the file *never*
gets deleted! It's as if the Session_OnEnd method was not
being called at all. Even if I just start a session,
create the file, and then quit the session, not starting
another one for the next hour, the file remains.
Therefore I also tried a simple test of creating a
recordset in the Session_OnEnd method, which inserts a
test value into a database table - but this value doesn't
get inserted when I quit the session. Is there something
else that needs to be done here? Here's my Session_OnEnd
method (with the test recordset):
Sub Session_OnEnd
Dim fs
Set fs = Server.CreateObject
("Scripting.FileSystemObject")
Dim tmp
Set tmp = Session("massGeneratedLetter")
Set cmdTmp = Server.CreateObject("ADODB.Command")
Set rsTmp = Server.CreateObject("ADODB.Recordset")
Set cmdTmp.ActiveConnection = DBConn
rsTmp.Close
Set rsTmp.Source = cmdTmp
cmdTmp.CommandType = 1
cmdTmp.CommandTimeout = 60
cmdTmp.CommandText = "INSERT INTO Tmp_SessionTest
(tmpVar)VALUES ('success!')"
rsTmp.CacheSize = 10
rsTmp.CursorType = 3
rsTmp.CursorLocation = 3
rsTmp.LockType = 3
rsTmp.Open
rsTmp.Requery
fs.DeleteFile Server.MapPath(tmp)
End Sub
Do I need to call the Session_OnEnd method somewhere in
order for it to execute?
Thanks much!
- JPA
Quote:
>-----Original Message-----
>The session ends from the server's perspective when it
times out, by default after 15 minutes of no client
access. Once the user closes the browser, the sessionid
cookie is gone (it's kept in memory only), so if they come
back to the site even within the timeout period, they
start a completely new session.
Quote:
>What you are doing isn't session specific, only user
specific. You will have problems if the user closes the
browser and starts it again and comes back within 15
minutes. The prior session will timeout and delete the
file while the new session is still active. I've done the
same kind of thing you are doing with temp files created
for the user that use the session.sessionid as part of the
temp file name.
Quote:
>--
>Michael Harris
>Microsoft.MVP.Scripting
Quote:
>> Thanks, Michael.
>> What does it mean that a session has ended? If I close
>> all the browser windows associated with the
application,
>> will that end the session? (I tried that but my file
>> didn't get deleted from its location, despite the
change
>> to fs.DeleteFile Server.MapPath(tmp) ).
>> Do I need to call the function Session_OnEnd()
somewhere?
>> If so, where?
>> Thanks a lot!
>> - JPA
>> >-----Original Message-----
>> >fs.DeleteFile Server.MapPath(tmp)
>> >--
>> >Michael Harris
>> >Microsoft.MVP.Scripting
>> >> Hi--
>> >> This is regarding ASP code in the Global.asa file.
>> >> I create a Session variable in the Session_OnStart
sub-
>> >> routine, as follows:
>> >> Session("massGeneratedLetter")
>> >> = "/views/massGeneratedLetters/Letters_" & Session
>> >> ("UserID") & ".rtf"
>> >> This is a pointer to a filename. The filename is
>> specific
>> >> to the username, due to the previously created
Session
>> >> ("UserID") variable in the filename. This file is
>> created
>> >> by a user when she performs a certain action within
the
>> >> website.
>> >> When the session ends, I'm trying to delete the file
>> >> created by the user, with the following code:
>> >> Sub Session_OnEnd
>> >> Dim fs
>> >> Set fs = CreateObject("Scripting.FileSystemObject")
>> >> Dim tmp
>> >> tmp = Session("massGeneratedLetter")
>> >> fs.DeleteFile(tmp)
>> >> End Sub
>> >> However, I tested it out and didn't see the file
>> deleted
>> >> from the directory. What does it mean that a
session
>> has
>> >> ended? Is the above code correct to delete files
>> created
>> >> by different users, depending on their sessions?
>> >> Thanks in advance,
>> >> - JPA
>> >.
>.