Here is a simple method using VBScript:
<% Session("foo") = "bar" Session("blat") = "splunge" For Each Item In Session.Contents Response.Write Item & " = " & Session(Item) Response.Write "<br>" Next %> |
In JScript, the syntax is slightly different:
<script language=JScript runat=server> Session("foo") = "bar"; Session("blat") = "splunge"; for ( s = new Enumerator(Session.Contents); !s.atEnd(); s.moveNext(); ) { Response.Write(s.item() + " = "); Response.Write(Session(s.item()); Response.Write("<br>"); } </script> |
Now, the above examples assume you are only storing strings in your session variables. Here is an example in VBScript that shows how to process single-dimensional arrays (also assumed to store strings) and identify objects that are stored in the session (though we
know better than that, right?).
<% Set fso = CreateObject("Scripting.FileSystemObject") Set Session("fso") = fso dim myArray(1) myArray(0) = "foo" myArray(1) = "bar" Session("myArray") = myArray Session("username") = "Splunge" Session("password") = "BlatBar" For Each Item In Session.Contents If IsArray(Session(item)) Then Response.Write item & " is an array" ' if you know its structure, you can ' loop through like this: localArray = Session(item) For i = 0 To Ubound(localArray) Response.Write "<br> " & item Response.Write "(" & i & ") = " & localArray(i) Next Elseif IsObject(Session(item)) Then Response.Write item & " is an object" ' though we know we're not supposed to ' store objects in the session, right? Else Response.Write item & " = " & Session(item) End If Response.Write "<br>" Next set session("fso") = nothing set fso = nothing %> |
And here is an example using JScript.
<script language=JavaScript runat=server> var fso = new ActiveXObject("Scripting.FileSystemObject"); Session("fso") = fso; var myArray = new Array(); myArray.push("foo"); myArray.push("bar"); Session("myArray") = myArray; Session("username") = "Splunge"; Session("password") = "BlatBar"; for ( s = new Enumerator(Session.Contents); !s.atEnd(); s.moveNext() ) { var sn = s.item(); var si = Session.Contents(s.item()); // we have to bring Session elements local // so we can access the constructor. var c = (si.constructor) ? si.constructor.toString() : "obj"; if (c.indexOf("Array()") != -1) { Response.Write(sn + " is an array"); // again, if you know the structure for (var i=0; i < si.length; i++) { Response.Write("<br> " + sn); Response.Write("[" + i + "] = " + si[i]); } } else if (c == "obj") { Response.Write(sn + " is an object"); } else { Response.Write(sn + " = " + si); } Response.Write("<br>"); } </script> |