search
HomeBackend DevelopmentC#.Net TutorialIntroducing three methods of Session object in ASP

In ASP, there are two internal objects that can store some information. They are the Application object and the Session object. The Application object is for the entire application period and is shared by all users who visit the website. , and Session is for the duration of the session, it only exists for the current user.

Introduction to the Session Object

When you work with an application on your computer, you open it, make changes, and then close it, much like a conversation (Session). The computer knows who you are, and it knows when you open and close applications. However, problems arise on the Internet, because HTTP addresses cannot maintain status, and the web server does not know who you are and what you have done.

The main purpose of the Session object is to store some information for each user who visits the website. For example, when the user logs in, we can add it to the user's Session Store information to identify that the current user is logged in.

The principle of Session is this. When a user visits the website for the first time, IIS assigns an identifier to the user. This identifier is a long random string. , this random string is called SessionID, and then the server sends it to the client and saves it in Cookies. When the user visits other pages on the server, the server obtains the SessionID and obtains the SessionID from the memory. Relevant data is placed in the collection of Session objects.

Contents collection

We can store some information about the current user in this collection. For example, the following code shows how to store and read data:

<%
&#39;名字为username的Session集合中存储了一个“ZhangSan”字符串
Session.Contents("username") = "ZhangSan"
Dim UserName
&#39;读取Session中的数据,可以省略Contents&#39;
UserName = Session.Contents("username")和下面一样
UserName = Session("username")
Response.Write("<h2 id="nbsp-nbsp-UserName-nbsp-nbsp">" & UserName & "</h2>")
%>

Session object There are three methods (Contents.Remove, Contents.RemoveAll, Abandon), which are used to delete the data in the Session collection or abandon the current Session.

First example(SessionContents.asp) We will demonstrate how to use the Remove and RemoveAll methods. The code is as follows:

...<h3 id="当前SessionID值为-nbsp-Session-SessionID">当前SessionID值为 <%=Session.SessionID%></h3><h3 id="Session中存储数据">Session中存储数据</h3><%&#39;利用 Contents.Count 遍历 Session 的过程Sub Traversal_P() 
  Dim i  For i = 1 To Session.Contents.Count
    Response.Write("Session(""" & Session.Contents.key(i) & """) = " & Session.Contents(i))
    Response.Write("<br>")  NextEnd Sub&#39;For Each 遍历 Session.Contents 集合 Sub Traversal_E()  Dim x  For Each x In Session.Contents 
    Response.Write("Session(""" & x & """) = " & Session(x))
    Response.Write("<br>")  NextEnd Sub&#39;Session.Contents中存储了多个数据,如下Session.Contents("username") = "ZhangSan"Session.Contents("password") = "12345678"Session.Contents("date")="2015/08/14"Session.contents("author")="pchmonster"&#39;遍历 Contents 集合Traversal_E()%><hr><h3 id="删除名为username的数据">删除名为username的数据</h3><%&#39;删除 username 数据Session.Contents.Remove("username")&#39;重新遍历 Contents 集合Traversal_P()%><hr><h3 id="删除所有的Session数据">删除所有的Session数据</h3><%&#39;删除所有的数据Session.Contents.RemoveAll()
Traversal_E()%>...

The above code will be displayed as follows after running:

Introducing three methods of Session object in ASP

These codes demonstrate two methods of traversing the Session.Contents collection, please take a closer look.

The second example (SessionAbandon.asp) demonstrates the effect of the Abandon method. Through the demonstration, we can see that the difference between the RemoveAll method and the Abandon method is that RemoveAll only deletes the current collection. But the client still uses the same SessionID (the SessionID remains unchanged in the first example). After the Abandon method is called, the Session collection can still be accessed on the current page. After the page is closed or refreshed, the previous Session will be deleted (the SessionID will change in this example).

The code is as follows:

<%&#39;Abandon的使用后,在当前页面仍可以访问Session集合,关闭页面或刷新后&#39;会使Session被删除,SessionID也就会改变Session.Abandon()&#39;首先我们要记录一下SessionID的值,存放到Cookies中Dim numVisits, SID
Response.Cookies("numVisits").Expires = DateAdd("d", 10, Now)
Response.Cookies("SID").Expires = DateAdd("d", 10, Now)
SID = Request.Cookies("SID")
numVisits = Request.Cookies("numVisits")If numVisits = "" or SID = "" Then
  &#39;如果是第一次运行该页面,则记录当前Sessio nID值  Response.Cookies("numVisits") = 1
  Response.Cookies("SID") = Session.SessionID%>
  <h3 id="您这是第一次访问该页面-当前页面的SessionID为">您这是第一次访问该页面,当前页面的SessionID为</h3>
  <h2><%=Session.SessionID%></h2><%Else%>
  <hr>
  <h3 id="您这是第-numVisits-次访问该页面-当前页面的SessioID为">您这是第<%=numVisits%>次访问该页面,当前页面的SessioID为</h3>
  <h2><%=Session.SessionID%></h2>
  <h3 id="您第一次访问时的SessionID为">您第一次访问时的SessionID为</h3>
  <h2><%=Request.Cookies("SID")%></h2><%
  numVisits = numVisits + 1
  Response.Cookies("numVisits") = numVisitsEnd If%>

The first time you run this page, the current SessionID will be recorded in Cookies, as shown below:

Introducing three methods of Session object in ASP

After refreshing the page multiple times or reopening it, because of the Abandon method, the Session will be deleted and the SessionID will keep changing, as shown below:

Introducing three methods of Session object in ASP

CodePage, SessionID, Timeout attributes

CodePage attribute defines the character set of the output content of the current page. The character set here is represented by numbers. For example,

936 means Chinese Simplified (GB2312), Simplified Chinese

950 means Chinese Traditional (Big5), Traditional Chinese

65001 means Unicode (UTF-8)

Special instructions

Applies to all static strings
Response.CodePage, Session.CodePage applies to all dynamic output The scope of string
Response.CodePage is only in a single response
Session.CodePage is in the scope of all responses in a session

SessionID 属性可以获得当前用户的 SessionID,有时候在客户端浏览器不支持 Cookies 的情况下,你可以将 SessionID 附加在客户端的 QueryString 变量中,从而标识每一个客户端。

Timeout 属性用于设定客户的 Session 超时期。客户对于 SessionID 并不是长期占有的,在其一段时间内没有和服务器端进行任何交互后,服务器端将放弃该 Session。

下面的代码(SessionCST.asp)中将演示这个三个属性的使用方法,代码如下:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%Session.CodePage = 65001&#39;作用于所有动态输出的字符串%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CodePage、SessionID、TimeOut属性的应用</title>
</head>
<body>
<h3 id="当前页面使用的CodePage是">当前页面使用的CodePage是:</h3>
<h2><%=Session.CodePage%></h2>
<hr>
<h3 id="当前页面的SessionID是">当前页面的SessionID是:</h3>
<h2><%=Session.SessionID%></h2>
<hr>
<h3 id="当前页面Session默认超时时间为">当前页面Session默认超时时间为:</h3>
<h2 id="Session-Timeout-分钟"><%=Session.Timeout%>分钟</h2>
</body>
</html>

运行后,效果如下:

Introducing three methods of Session object in ASP

【相关推荐】

1. ASP免费视频教程

2. 详解ASP中Session的使用技巧

3. ASP session简单示例

4. 关于ASP中session的详细介绍

5. 教你解决ASP session丢失的方法

The above is the detailed content of Introducing three methods of Session object in ASP. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Community of C# .NET Developers: Resources and SupportThe Community of C# .NET Developers: Resources and SupportMay 06, 2025 am 12:11 AM

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The C# .NET Advantage: Features, Benefits, and Use CasesThe C# .NET Advantage: Features, Benefits, and Use CasesMay 05, 2025 am 12:01 AM

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.

Is C# Always Associated with .NET? Exploring AlternativesIs C# Always Associated with .NET? Exploring AlternativesMay 04, 2025 am 12:06 AM

C# is not always tied to .NET. 1) C# can run in the Mono runtime environment and is suitable for Linux and macOS. 2) In the Unity game engine, C# is used for scripting and does not rely on the .NET framework. 3) C# can also be used for embedded system development, such as .NETMicroFramework.

The .NET Ecosystem: C#'s Role and BeyondThe .NET Ecosystem: C#'s Role and BeyondMay 03, 2025 am 12:04 AM

C# plays a core role in the .NET ecosystem and is the preferred language for developers. 1) C# provides efficient and easy-to-use programming methods, combining the advantages of C, C and Java. 2) Execute through .NET runtime (CLR) to ensure efficient cross-platform operation. 3) C# supports basic to advanced usage, such as LINQ and asynchronous programming. 4) Optimization and best practices include using StringBuilder and asynchronous programming to improve performance and maintainability.

C# as a .NET Language: The Foundation of the EcosystemC# as a .NET Language: The Foundation of the EcosystemMay 02, 2025 am 12:01 AM

C# is a programming language released by Microsoft in 2000, aiming to combine the power of C and the simplicity of Java. 1.C# is a type-safe, object-oriented programming language that supports encapsulation, inheritance and polymorphism. 2. The compilation process of C# converts the code into an intermediate language (IL), and then compiles it into machine code execution in the .NET runtime environment (CLR). 3. The basic usage of C# includes variable declarations, control flows and function definitions, while advanced usages cover asynchronous programming, LINQ and delegates, etc. 4. Common errors include type mismatch and null reference exceptions, which can be debugged through debugger, exception handling and logging. 5. Performance optimization suggestions include the use of LINQ, asynchronous programming, and improving code readability.

C# vs. .NET: Clarifying the Key Differences and SimilaritiesC# vs. .NET: Clarifying the Key Differences and SimilaritiesMay 01, 2025 am 12:12 AM

C# is a programming language, while .NET is a software framework. 1.C# is developed by Microsoft and is suitable for multi-platform development. 2..NET provides class libraries and runtime environments, and supports multilingual. The two work together to build modern applications.

Beyond the Hype: Assessing the Current Role of C# .NETBeyond the Hype: Assessing the Current Role of C# .NETApr 30, 2025 am 12:06 AM

C#.NET is a powerful development platform that combines the advantages of the C# language and .NET framework. 1) It is widely used in enterprise applications, web development, game development and mobile application development. 2) C# code is compiled into an intermediate language and is executed by the .NET runtime environment, supporting garbage collection, type safety and LINQ queries. 3) Examples of usage include basic console output and advanced LINQ queries. 4) Common errors such as empty references and type conversion errors can be solved through debuggers and logging. 5) Performance optimization suggestions include asynchronous programming and optimization of LINQ queries. 6) Despite the competition, C#.NET maintains its important position through continuous innovation.

The Future of C# .NET: Trends and OpportunitiesThe Future of C# .NET: Trends and OpportunitiesApr 29, 2025 am 12:02 AM

The future trends of C#.NET are mainly focused on three aspects: cloud computing, microservices, AI and machine learning integration, and cross-platform development. 1) Cloud computing and microservices: C#.NET optimizes cloud environment performance through the Azure platform and supports the construction of an efficient microservice architecture. 2) Integration of AI and machine learning: With the help of the ML.NET library, C# developers can embed machine learning models in their applications to promote the development of intelligent applications. 3) Cross-platform development: Through .NETCore and .NET5, C# applications can run on Windows, Linux and macOS, expanding the deployment scope.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.