Whats up with my Session Killing function?

C

Colin Steadman

I have created a function to kill all session variables
that aren't in a safe list. This is the function -

Sub PurgeSessionVariables
For Each Item In Session.Contents
Select Case Trim(Item)
Case "Authenticated"
Case "CI_CODE"
Case "organisation_description"
Case "location_description"
Case "company"
Case Else
response.write "Killing:" & Item & "<br>"
Session.Contents.Remove(Item)
End Select
Next
End Sub

If I refresh the page that calls this function I get this -

Killing: general_problems
Killing: phone_problems
Killing: title
Killing: preferred_forename
Killing: ni
Killing: initials
Killing: dob_dd
Killing: dob_yyyy
Killing: doe_mm
Killing: memorabledate_dd
Killing: memorabledate_yyyy
Killing: sex
Killing: phone
Killing: fax
Killing: mobex
Killing: roles

If I then hit F5 to refresh I see this -

Killing: personal_problems
Killing: forenames
Killing: jobtitle
Killing: dob_mm
Killing: doe_yyyy
Killing: mothers_maiden_name
Killing: mobile
Killing: pager

If I then hit F5 again to refresh I see this -

Killing: roles_problems
Killing: surnameinitials
Killing: memorabledate_mm
Killing: altphone

If I then hit F5 again to refresh I see this -

Killing: surname
Killing: empstatus

If I then hit F5 again to refresh I see this -

Killing: doe_dd

And finally if I then hit F5 again no further variable are
deleted. This is very odd as all unwanted session
variables should be removed on the first refresh. I've
gone through my code line by line several times but I
really cant see whats going wrong... Any ideas?

TIA,

Colin
 
C

Chris Barber

Enumerating a loop to remove items from a collection is difficult since the
enumeration may skip items as the collection is reduced.
An 'easier' way is to remove the first item and then check the length again.

Try this:

Sub PurgeSessionVariables
Do Until Session.Contents.Count = 0
Item = Session.Contents.Item(1)
Select Case Trim(Item)
Case "Authenticated"
Case "CI_CODE"
Case "organisation_description"
Case "location_description"
Case "company"
Case Else
Response.Write "Killing:" & Item & "<br>"
Session.Contents.Remove Item
End Select
Loop
End Sub

Of course, I may be wrong so feel free to argue if it doesn't work (untested
as yet).

Cheers,

Chris.
"Colin Steadman"
<msdn@ColinSteadmanASYOUHAVEGUESSEDTHISISNOTPARTOFMYEMAILADDRESS.com.NORTHIS
BIT> wrote in message I have created a function to kill all session variables
that aren't in a safe list. This is the function -

Sub PurgeSessionVariables
For Each Item In Session.Contents
Select Case Trim(Item)
Case "Authenticated"
Case "CI_CODE"
Case "organisation_description"
Case "location_description"
Case "company"
Case Else
response.write "Killing:" & Item & "<br>"
Session.Contents.Remove(Item)
End Select
Next
End Sub

If I refresh the page that calls this function I get this -

Killing: general_problems
Killing: phone_problems
Killing: title
Killing: preferred_forename
Killing: ni
Killing: initials
Killing: dob_dd
Killing: dob_yyyy
Killing: doe_mm
Killing: memorabledate_dd
Killing: memorabledate_yyyy
Killing: sex
Killing: phone
Killing: fax
Killing: mobex
Killing: roles

If I then hit F5 to refresh I see this -

Killing: personal_problems
Killing: forenames
Killing: jobtitle
Killing: dob_mm
Killing: doe_yyyy
Killing: mothers_maiden_name
Killing: mobile
Killing: pager

If I then hit F5 again to refresh I see this -

Killing: roles_problems
Killing: surnameinitials
Killing: memorabledate_mm
Killing: altphone

If I then hit F5 again to refresh I see this -

Killing: surname
Killing: empstatus

If I then hit F5 again to refresh I see this -

Killing: doe_dd

And finally if I then hit F5 again no further variable are
deleted. This is very odd as all unwanted session
variables should be removed on the first refresh. I've
gone through my code line by line several times but I
really cant see whats going wrong... Any ideas?

TIA,

Colin
 
P

Phillip Windell

That doesn't kill the Session, it only clears the contents of the
variables. If you want to kill the Session it is:

Session.Abandon


--

Phillip Windell [CCNA, MVP, MCP]
(e-mail address removed)
WAND-TV (ABC Affiliate)
www.wandtv.com

"Colin Steadman"
<msdn@ColinSteadmanASYOUHAVEGUESSEDTHISISNOTPARTOFMYEMAILADDRESS.com.N
ORTHISBIT> wrote in message
news:[email protected]...
 
P

Phillip Windell

Nevermind. I was judging it from the subject line and though you
wanted to "Kill" the Session. Your subject line is misleading.


--

Phillip Windell [CCNA, MVP, MCP]
(e-mail address removed)
WAND-TV (ABC Affiliate)
www.wandtv.com

"Colin Steadman"
<msdn@ColinSteadmanASYOUHAVEGUESSEDTHISISNOTPARTOFMYEMAILADDRESS.com.N
ORTHISBIT> wrote in message
news:[email protected]...
 
C

Colin Steadman

Enumerating a loop to remove items from a collection is
difficult since the
enumeration may skip items as the collection is reduced.
An 'easier' way is to remove the first item and then check the length again.

Try this:

Sub PurgeSessionVariables
Do Until Session.Contents.Count = 0
Item = Session.Contents.Item(1)
Select Case Trim(Item)
Case "Authenticated"
Case "CI_CODE"
Case "organisation_description"
Case "location_description"
Case "company"
Case Else
Response.Write "Killing:" & Item & "<br>"
Session.Contents.Remove Item
End Select
Loop
End Sub

Of course, I may be wrong so feel free to argue if it doesn't work (untested
as yet).


My session contents count would never reach zero. When
purging session variables I want to preserve a few of
them, CI_CODE and AUTHENTICATED are essential. But you
put me on the right track.

What I've done is create a safe list variable that can be
added to, and then creating an array from it.

It then does pretty much the same thing as before, but
keeps chugging away until the session contents count (that
you made me aware of) equals the number of items in the
array of safe items.

It feels dirty, but its doing the job. If I've got time
later I'll see if it can be improved.

Thanks for your help!

Colin

PS Heres the function if your interested:

'===================================================
'Purge session variables
'===================================================

Sub PurgeSessionVariables

'Create array of safe items
safeList = "Authenticated, CI_CODE,
organisation_description, location_description, company"
arrarySafeList = split(safeList, ",",-1,1)
Set safeList = Nothing

'Loop through Session variable + kill anything not in
safe array
Do Until Session.Contents.Count = (UBound
(arrarySafeList, 1) + 1)
For Each Item In Session.Contents
inSafeList = False
For i = 0 To UBound(arrarySafeList, 1)
If Item = Trim(arrarySafeList(i)) Then inSafeList
= True
Next
If Not inSafeList Then Session.Contents.Remove Item
Next
Loop

Set arrarySafeList = Nothing

End Sub
 
C

Chris Barber

Unfortunately collections don't behave like arrays, once you remove an
element then everything sort of shuffles up and if you are in the middle of
an enumerated for each loop then it can miss items.

Maybe you can use the .Key property of each element to create an array of
key values to be removed and do it based on that?

That might feel a little better than the current method?

Hope this helps,

Chris.

Colin Steadman said:
Enumerating a loop to remove items from a collection is difficult since the
enumeration may skip items as the collection is reduced.
An 'easier' way is to remove the first item and then check the length again.

Try this:

Sub PurgeSessionVariables
Do Until Session.Contents.Count = 0
Item = Session.Contents.Item(1)
Select Case Trim(Item)
Case "Authenticated"
Case "CI_CODE"
Case "organisation_description"
Case "location_description"
Case "company"
Case Else
Response.Write "Killing:" & Item & "<br>"
Session.Contents.Remove Item
End Select
Loop
End Sub

Of course, I may be wrong so feel free to argue if it doesn't work (untested
as yet).


My session contents count would never reach zero. When
purging session variables I want to preserve a few of
them, CI_CODE and AUTHENTICATED are essential. But you
put me on the right track.

What I've done is create a safe list variable that can be
added to, and then creating an array from it.

It then does pretty much the same thing as before, but
keeps chugging away until the session contents count (that
you made me aware of) equals the number of items in the
array of safe items.

It feels dirty, but its doing the job. If I've got time
later I'll see if it can be improved.

Thanks for your help!

Colin

PS Heres the function if your interested:

'===================================================
'Purge session variables
'===================================================

Sub PurgeSessionVariables

'Create array of safe items
safeList = "Authenticated, CI_CODE,
organisation_description, location_description, company"
arrarySafeList = split(safeList, ",",-1,1)
Set safeList = Nothing

'Loop through Session variable + kill anything not in
safe array
Do Until Session.Contents.Count = (UBound
(arrarySafeList, 1) + 1)
For Each Item In Session.Contents
inSafeList = False
For i = 0 To UBound(arrarySafeList, 1)
If Item = Trim(arrarySafeList(i)) Then inSafeList
= True
Next
If Not inSafeList Then Session.Contents.Remove Item
Next
Loop

Set arrarySafeList = Nothing

End Sub
 
G

Guest

-----Original Message-----
Unfortunately collections don't behave like arrays, once you remove an
element then everything sort of shuffles up and if you are in the middle of
an enumerated for each loop then it can miss items.

Maybe you can use the .Key property of each element to create an array of
key values to be removed and do it based on that?

That might feel a little better than the current method?

Hope this helps,

Chris.


Thats a good idea. I'll make a comment in the script and
come back to it in maintenance.

Cheers,

Colin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top