Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of session in ASP

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces ASP session sample analysis, the text is very detailed, has a certain reference value, interested in small partners must read!

Session objects are used to store user information. Variables stored in session objects hold information about a single user and are available to all pages in an application.

Session object

When you operate an application, you open it, make changes, and then close it. It was like a conversation. The computer knows who you are. It knows when you open and close apps. But there's a problem with the Internet: Because HTTP addresses don't persist, web servers don't know who you are or what you do.

ASP solves this problem by creating a unique cookie for each user. Cookies are sent to the client and contain information that identifies the user. This interface is called a Session object.

Session objects are used to store information about users or to change settings for a user's session. Variables stored in the session object hold information about a single user and are available to all pages in the application. The information stored in a 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?

The Session starts with:

* When a new user requests an ASP file and the Global.asa file references the Session_OnStart subroutine;

* When a value is stored in the Session variable;

* When a user requests an ASP file and Global.asa instantiates an object through the scope of the session using tags;

When does the Session end?

If the user does not request or refresh the page in the application within the specified time, the session ends. The default is 20 minutes.

If you want to set the timeout interval longer or shorter, you can set the Timeout property.

The following example sets a timeout interval of 5 minutes:

To end the session immediately, use the Abandon method:

Note: The main question when using sessions is when they should end. We don't know if the user's most recent request was the last request. So we don't know how long we should let the session"live." Waiting too long for an idle session drains the server's resources. However, if the session is deleted prematurely, the user will have to start again and again because the server has deleted all the information. Finding the right timeout interval is difficult.

Tip: If you are using session variables, do not store large amounts of data in them.

Store and retrieve session variables

The biggest advantage of the Session object is that it can store variables in it for subsequent web pages to read, and its application range is very wide.

The following example assigns "Donald Duck" to a session variable named username and "50" to a session variable named age:

Once the value is stored in the session variable, it can be used by any page in an ASP application:

Welcome

The above line returns the result: "Welcome Donald Duck."

You can also save user parameters in a session object and access them to decide what page to return to the user.

The following example specifies that if the user uses a low display resolution, a plain text version of the page is returned:

This is the text version of the page This is the multimedia version of the page

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, the session variable "sale" is removed:

Results:

usernameage

If you need to know the number of items in the contents collection, you can use the count attribute:

Results:

Session variables: 2Donald Duck50

Traversing the StaticObjects collection

You can view the values of all objects stored in a session object by looping through the StaticObjects collection:

The above is "ASP session sample analysis" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report