遍历SESSION或APPLICATION对象

王朝other·作者佚名  2006-11-24
宽屏版  字体: |||超大  

Have you ever wondered how to list all of your Session and Application variables? Both the Session and Application objects provide the Contents collection, which contains all of the session and application variables that are not objects (i.e., arrays, strings, integers, etc.).

You can loop through this collection using the For Each ... Next. Since arrays are included in the Contents collection, we need to determine if the current variable is an array, using the IsArray function. If the variable is an array, we need to loop through the array one element at a time, displaying each element. If the variable is not an array, we need to simply display the variable.

<%@ Language=VBScript %>

<% Option Explicit %>

<%

'How many session variables are there?

Response.Write "There are " & Session.Contents.Count & _

" Session variables

"

Dim strName, iLoop

'Use a For Each ... Next to loop through the entire collection

For Each strName in Session.Contents

'Is this session variable an array?

If IsArray(Session(strName)) then

'If it is an array, loop through each element one at a time

For iLoop = LBound(Session(strName)) to UBound(Session(strName))

Response.Write strName & "(" & iLoop & ") - " & _

Session(strName)(iLoop) & "

"

Next

Else

'We aren't dealing with an array, so just display the variable

Response.Write strName & " - " & Session.Contents(strName) & "

"

End If

Next

%>

You can list the Application variables by simply changing each instance of the word Session in the code above with the word Application!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有