How to get all the session variables

Source: Internet
Author: User
Tags array object iis modify variables variable
session| variable |session in the program debugging, sometimes need to know how many session variables are in use, their value? Because the Session object provides a collection called contents (Collection), we can use the for ... Each loop to achieve the goal:
Dim StrName, Iloop
For each strName in session.contents
Response.Write StrName & "-" & Session.Contents (strName) & "<BR>"
Next

In general, the code above can work very well. However, when the session variable is an object or an array, the printed result is incorrect.

So we modify the code as follows:
' First look at how many session variables are in use?

Response.Write "There are" & Session.Contents.Count & _
"Session Variables<p>"
Dim StrName, Iloop
' Use for Each loop to see Session.Contents
' If the session variable is an array?
If IsArray (Session (StrName)) Then
' Circulate every element of a set of print
For iloop = LBound (Sessions (StrName)) to UBound (session (StrName))
Response.Write StrName & "(" & Iloop & ")-" & _
Session (StrName) (iloop) & "<BR>"
Next
Else
' Otherwise, simply print the value of the variable
Response.Write StrName & "-" & Session.Contents (strName) & "<BR>"
End If
Next

Session variables sometimes don't work, why?

There are many possibilities:
First, the session will fail if the client does not allow cookies to operate. Because the session is dependent on cookies.
Second, the session has the expiration time setting. The default setting is 20 minutes. You can modify it this way: Web directory-> Properties-> Virtual directory-> application settings-> Configuration App O Ptions-> Session Timeout
Or in the ASP, write this code: SESSION.TIMEOUT=60.
Third, the session is related to the specific Web application. If a user browses from/products/default.asp to/jobs/default.asp, it can also cause a session to be recreated.

How do I clear a session variable that is no longer needed but not invalidate the session?
In the ASP3.0:
Session.Contents.Remove "Variable name"
You can clear a variable.
In the ASP2.0:
Set session ("Variable name") =null
You can clear variables.
In the ASP3.0,
Session.Contents.RemoveAll
You can clear all of the session variables and Session.Abandon, and none of the above methods will expire or invalidate the current session.

What does the <%@ enablesessionstate=true%> at the top of the ASP page mean?
IIS uses a technique called session tracking to ensure that each session variable is available on each page. When a user accesses an ASP page, IIS prepares the session variables for the page first, which, of course, has a performance impact. (The cost of using session variables is always high!) )
If you have 100 pages, and only 5 pages use the session, then, for overall performance, you only need to set up on those 5 pages:
<%@ enablesessionstate=true%>
And the other pages are set to:
<%@ Enablesessionstate=false%>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.