ASP Introductory Tutorial: Introduction to ASP Session objects

Source: Internet
Author: User
Tags change settings

ASP Introductory Tutorial: Introduction to ASP Session objects

The session object is used to store information, or to change settings for a user meeting. Variables stored in the Session object hold information to a single user and are provided to all Web pages in an application.


-------------------------------------------------- ------------------------------

Session Object
When you are working with an application, you open it, make some changes, and then turn it off. It's much like meeting. The computer knows who you are. It knows when you start the application when you are finished. However, there is a problem on the Internet: the Web server does not know who you are and how you do it because the HTTP address does not remain state.

ASP technology solves this problem by creating a unique cookie for each user. The cookie is sent to the client, it contains the information that determines the user. This interface is called a Session object.

The session object is used to store information, or to change settings for a user meeting. Variables stored in the Session object hold information to a single user and are provided to all Web pages in an application. The common information stored in the session variables is the name, number, and preferences. The server creates a new session object for each new user and destroys the sessions object when the meeting ends.


-------------------------------------------------- ------------------------------

When does a session begin?
At the beginning of the session:

A new user requests an ASP file, and Global.asa file contains Session_OnStart program
A value is stored in a session variable
The user requests an ASP file and global.asa the object and scope meeting of the file using <OBJECT> tag instance

-------------------------------------------------- ------------------------------

When does a session end?
A meeting ends if the user has not yet requested or refreshed the page's application during the stipulated period. By default, this is 20 minutes.

If you want to set a time-out interval that is shorter than or longer than the default, you can set the Timeout property.

The following example sets a time-out interval of 5 minutes:

<%
session.timeout=5
%>
  
  

To end a meeting, you can use the Abandon method immediately:

<%
Session.Abandon
%>
    
    

Note: The main problem is when the meeting should stop. We don't know the user's request last year or not. So we don't know how long we should keep the meeting "alive". Wait too long the meeting is taking advantage of idle resources on the server, but if the meeting is deleted too fast the user must start over, because the server has removed all the information. It can be difficult to find the appropriate timeout interval!

Tip: If you are using session variables, store a small amount of data in them.


-------------------------------------------------- ------------------------------

Storing and retrieving session variables
The most important thing for the session object is that you can store the variables in it.

The following example sets the user name of the session variable to "Donald Duck" and the conversation Variable age "50":

<% session
("username") = ' Donald Duck ' session
(' age ') =50
%>
      
      

The value is stored in a session variable it can be from any Web page in an ASP application:

Welcome <%response.write (Session ("username"))%>
        
        

The return of the line above: "Welcome to Donald Duck".

You can also store user preferences in the Session object, and then get the priority Selection page returned to the user.

The following example specifies a text-only version of the Web page, if the user has a lower screen resolution:

<%if session ("screenres") = ' low ' then%> This is the
  text version of the page
<%Else%>
  This is the Multimedia version of the page
<%end if%>

To delete a session variable
The collected content contains all session variables.

It is possible to put a session variable with the Delete method.

The following example deletes the session variable "buy and sell" if the value of the session variable "age" is less than 18:

<%
If session.contents (' age ') <18 then
  Session.Contents.Remove ("Sale") end
If
%>
           
           

To delete all variables at one meeting, use the RemoveAll method:

<%
Session.Contents.RemoveAll ()
%>

Loop through the collected content
The collected content contains all session variables. You can iterate through the collection of directories and see what is stored in it:

<% session
("username") = ' Donald Duck ' session
(' age ') =50
Dim i for each
i in session.contents
  Response.Write (i & "<br/>")
Next
%>
Result
Username Age

  
                
                

If you are not aware of some items in the collection of catalogs, 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
%>


   
                 
                 
Output on duty.
Session Variables:2
Donald Duck
50

You can gather through the ring staticobjects and see the values of all objects stored in the Session object:
<%
Dim i For each
i in session.staticobjects
  Response.Write (i & "<br/>")
Next
% >
Reprint please indicate from www.111cn.net/asp/asp.html


Related Article

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.