By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

Whats up with my Session Killing function?

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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"
<ms**@ColinSteadmanASYOUHAVEGUESSEDTHISISNOTPARTOF MYEMAILADDRESS.com.NORTHIS
BIT> wrote in message news:02****************************@phx.gbl...
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


Jul 19 '05 #2

P: n/a
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]
pw******@wandtv.com
WAND-TV (ABC Affiliate)
www.wandtv.com

"Colin Steadman"
<ms**@ColinSteadmanASYOUHAVEGUESSEDTHISISNOTPARTOF MYEMAILADDRESS.com.N
ORTHISBIT> wrote in message
news:02****************************@phx.gbl...
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

Jul 19 '05 #3

P: n/a
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]
pw******@wandtv.com
WAND-TV (ABC Affiliate)
www.wandtv.com

"Colin Steadman"
<ms**@ColinSteadmanASYOUHAVEGUESSEDTHISISNOTPARTOF MYEMAILADDRESS.com.N
ORTHISBIT> wrote in message
news:02****************************@phx.gbl...
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

Jul 19 '05 #4

P: n/a
>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 (untestedas 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


Jul 19 '05 #5

P: n/a
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" <ms**@colinsteadmanNOtParTOfEmailAdDRess.comNORTHI SBIT>
wrote in message news:0a****************************@phx.gbl...
Enumerating a loop to remove items from a collection is difficult since theenumeration 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 (untestedas 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



Jul 19 '05 #6

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

Maybe you can use the .Key property of each element to create an array ofkey 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
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.