ASP Session 对象
- 上一页 ASP Cookies
- 下一页 ASP Application
Το αντικείμενο Session χρησιμοποιείται για τη αποθήκευση πληροφοριών χρήστη. Οι μεταβλητές που αποθηκεύονται στο αντικείμενο session διατηρούν πληροφορίες για έναν μοναδικό χρήστη και είναι διαθέσιμες σε όλες τις σελίδες μιας εφαρμογής.
Αντικείμενο Session
Όταν δουλεύετε με μια εφαρμογή, ανοίγετε την, κάνετε μερικές αλλαγές και την κλείνετε. Αυτό είναι πολύ σαν μια συζήτηση (Session). Ο υπολογιστής ξέρει ποιος είστε. Είναι σαφές πότε ανοίγετε και κλείνετε την εφαρμογή. Ωστόσο, υπάρχει ένα πρόβλημα στο Διαδίκτυο: λόγω της αδυναμίας του HTTP να διατηρήσει την κατάσταση, ο διακομιστής ιστού δεν ξέρει ποιος είστε και τι έχετε κάνει.
Το ASP επιλύει αυτό το πρόβλημα δημιουργώντας έναν μοναδικό cookie για κάθε χρήστη. Ο cookie αποστέλλεται στον πελάτη και περιέχει πληροφορίες που αναγνωρίζουν τον χρήστη. Αυτό το σύνδεσμο ονομάζεται αντικείμενο Session.
The Session object is used to store information about the user, or to change the settings for a user's session. The variables stored in the session object contain information about a single user and are available to all pages in the application. The information stored in the session object is usually name, id, and parameters. The server creates a new Session for each new user and revokes the Session object when the session expires.
When does the session start?
When does the session start?
- When a new user requests an ASP file, and the Global.asa file references the Session_OnStart subroutine;
- When a value is stored in a Session variable;
- When a user requests an ASP file, and Global.asa uses the <object> tag to instantiate an object through the session scope;
When does the session end?
If the user does not request or refresh the page within the specified time in the application, the session will end. The default value is 20 minutes.
If you want to set a longer or shorter timeout interval, you can set Timeout Property.
The following example sets a timeout interval of 5 minutes:
<% Session.Timeout=5 %>
To end the session immediately, you can use Abandon Method:
<% Session.Abandon %>
Note:The main problem when using session is to determine when they should end. We do not know whether the user's recent request is the final request. Therefore, we are not sure how long the session should "survive". Waiting too long for an idle session will exhaust the server's resources. However, if the session is deleted too early, the user will have to start over again and again, because the server has deleted all the information. Finding the appropriate timeout interval is very difficult.
Tip:If you are using session variables, do not store a large amount of data in them.
Storing and retrieving session variables
The biggest advantage of the Session object is that variables can be stored in it for subsequent web pages to read, and its application range is very wide.
The following example assigns the value "Donald Duck" to the session variable named username, and assigns the value "50" to the session variable named age:
<% Session("username")="Δονλντ Κουν" Session("age")=50 %>
Once a value is stored in the session variable, it can be used by any page in the ASP application:
Welcome <%Response.Write(Session("username"))%>
The result of the above line of code is: "Welcome Donald Duck".
You can also save user parameters in the session object and then access these parameters to decide what page to return to the user.
The following example specifies that if the user uses a low screen resolution, return the plain text version of the page:
<%If Session("screenres")="low" Then%> This is the text version of the page <%Else%> This is the multimedia version of the page <%End If%>
Remove session variable
The contents collection contains all session variables.
You can remove session variables by using the remove method.
In the following example, if the value of the session variable "age" is less than 18, remove the session variable "sale":
<% If Session.Contents("age")<18 then Session.Contents.Remove("sale") End If %>
If you need to remove all variables from the session, please use the RemoveAll method:
<% Session.Contents.RemoveAll() %>
Iterate over the contents collection
The contents collection contains all session variables. You can view the stored variables by iterating over the contents collection:
<% Session("username")="Δονλντ Κουν" Session("age")=50 dim i For Each i in Session.Contents Response.Write(i & "<br />") Next %>
Results:
username age
If you need to know the number of items in the contents collection, you can use the count property:
<% dim i dim j j=Session.Contents.Count Response.Write("Session variables: " & j) For i=1 to j Response.Write(Session.Contents(i) & "<br />") Next %>
Results:
Session variables: 2 Δονλντ Κουν 50
Προσπαθείται η διαδρομή της συλλογής StaticObjects
可通过循环 StaticObjects 集合,来查看存储在 session 对象中所有对象的值:
<% dim i For Each i in Session.StaticObjects Response.Write(i & "<br />") Next %>
- 上一页 ASP Cookies
- 下一页 ASP Application