Home >Backend Development >C#.Net Tutorial >Introducing 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:
<% '名字为username的Session集合中存储了一个“ZhangSan”字符串 Session.Contents("username") = "ZhangSan" Dim UserName '读取Session中的数据,可以省略Contents' UserName = Session.Contents("username")和下面一样 UserName = Session("username") Response.Write("<h2>" & 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>当前SessionID值为 <%=Session.SessionID%></h3><h3>Session中存储数据</h3><%'利用 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'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'Session.Contents中存储了多个数据,如下Session.Contents("username") = "ZhangSan"Session.Contents("password") = "12345678"Session.Contents("date")="2015/08/14"Session.contents("author")="pchmonster"'遍历 Contents 集合Traversal_E()%><hr><h3>删除名为username的数据</h3><%'删除 username 数据Session.Contents.Remove("username")'重新遍历 Contents 集合Traversal_P()%><hr><h3>删除所有的Session数据</h3><%'删除所有的数据Session.Contents.RemoveAll() Traversal_E()%>...
The above code will be displayed as follows after running:
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:
<%'Abandon的使用后,在当前页面仍可以访问Session集合,关闭页面或刷新后'会使Session被删除,SessionID也就会改变Session.Abandon()'首先我们要记录一下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 '如果是第一次运行该页面,则记录当前Sessio nID值 Response.Cookies("numVisits") = 1 Response.Cookies("SID") = Session.SessionID%> <h3>您这是第一次访问该页面,当前页面的SessionID为</h3> <h2><%=Session.SessionID%></h2><%Else%> <hr> <h3>您这是第<%=numVisits%>次访问该页面,当前页面的SessioID为</h3> <h2><%=Session.SessionID%></h2> <h3>您第一次访问时的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:
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:
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'作用于所有动态输出的字符串%> <!doctype html> <html> <head> <meta charset="utf-8"> <title>CodePage、SessionID、TimeOut属性的应用</title> </head> <body> <h3>当前页面使用的CodePage是:</h3> <h2><%=Session.CodePage%></h2> <hr> <h3>当前页面的SessionID是:</h3> <h2><%=Session.SessionID%></h2> <hr> <h3>当前页面Session默认超时时间为:</h3> <h2><%=Session.Timeout%>分钟</h2> </body> </html>
运行后,效果如下:
【相关推荐】
1. ASP免费视频教程
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!