Session
If you are using ASP 3.0 (the version of ASP-comes with Windows 2000/iis 5) then your can use the following syntax:
Session.Contents.Remove "Name"
Where name is the name of the session variable your wish to remove. Removing session variables in this way has its advantages by using the following method:
Session ("Name") = Null
Namely, the above method (setting a sessions variable to Null) is only removes the memory associated and the session variable itself. The Session object still maintains a reference to it, though. The session object with a Key/item pair, similar to the Scripting.Dictionary object. Therefore, using the Null method above, you are not removing the key from the session Contents collection that contains th E reference to the variable ...
With the ' Remove method ' We looked at the are removing both the key and item associated. There is also a removeall-can is used to scrap all of the sessions variables completely:
Session.Contents.RemoveAll
Again, the Remove and RemoveAll methods are new to ASP 3.0. If you are have ASP 2.0, you'll need to use the Null method
Deleting a subset of Session Variables
When using the Sessions to store variables in, I use a naming convention-for example, for all Customer related info I prefix The session variable with the substring Customer. (So for the CustomerID it would is customer.id, the Customer username would be customer.name, etc.) This is very useful when viewing the session objects as and you can to the related objects off.
The problem I had the other day is that I wanted to remove only those items from the session which were SW. So-I-used the following code:
'---------------------------------------
Session ("sw.1") = "Test 1"
Session ("sw.2") = "Test 2"
Session (' other ') = "other"
For each sessionitem in session.contents
If Left (sessionitem,3) = "SW." Then
Session.Contents.Remove (Sessionitem)
End If
Next
'---------------------------------------
This seems fine, but when it's run, what happens is-is-sw.1, removed but is-not sw.2. Why? I ' m not exactly sure, but I guess that the index is then reset so that sw.2 be now where sw.1 be, and seeing as we have I Terated past that item in the For each ... Next statement, the loop just moves to, and missing out sw.2 altogether! eek!
So to get round this I wrote the following function, which would properly delete all sessions variables that begin with a Specified substring:
'---------------------------------------
function sessionremoveselected (sitemprefix)
'////////////////
' Remove Selected Items starting with Sitemprefix
' from the session. E.g. SS. Would remove ss.id and
' ss.name but not CustomerID Returns True or False
' depending in whether any items where removed.
'---------------------------------------
' Sitemprefix [string]: Item Prefix
'//////////////////////////
Dim Arysession ()
Dim lcount, Lprefixlength
Dim sessionitem
Dim Blnresult
Lcount =-1
lprefixlength = Len (sitemprefix)
Blnresult = False
' temporarily store in array items to Remove
for each sessionitem in Session.Contents
if left (sessionitem,lprefixlength) = Sitemprefix then
Lcount = Lcount + 1
ReDim Preserve Arysession (Lcount)
Arysession (lcount) = Sessionitem
End If
Next
' Remove items
If IsArray (arysession) and lcount >= 0 Then
For lcount = LBound (arysession) to UBound (arysession)
Session.Contents.Remove (Arysession (lcount))
Next
Blnresult=true
End If
sessionremoveselected = Blnresult
End Function
'-------------------------------------------------