当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP > ASP Session

ASP
ASP 介绍
ASP Install
ASP 语法
ASP 变量
ASP Procedures
ASP Forms
ASP Cookies
ASP Session
ASP Application
ASP #include
ASP Global.asa
ASP Send e-mail
ASP Response
ASP Request
ASP Application
ASP Session
ASP Server
ASP Error
ASP FileSystem
ASP TextStream

ASP Session


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 970 ::
收藏到网摘: n/a

The Session object is used to store information about, or change settings for a user session. Variables stored in the Session object hold information about one single user, and are available to all pages in one application.
Session 对象是用来存储用户的Session信息以及用来改变用户Session的设置的。对于Session对象中的变量所存储的单个用户的信息,在同一请求下的所有页面都可以访问到它。


The Session object
Session对象

When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state.
当你运行一个应用程序,打开它,对它作了一些改变以后关闭它。这个过程就像在和程序进行交流。电脑知道你是谁,他明白你什么时候开始运行程序,什么时候结束。但是,在互联网上就会存在这样一个问题:网络服务器不知道你是谁,不知道你干了什么,因为HTTP地址不能保持(对所有的操作进行纪录)状态。

ASP solves this problem by creating a unique cookie for each user. The cookie is sent to the client and it contains information that identifies the user. This interface is called the Session object.
然而,ASP通过为每一个用户建立一个完全独立“Cookie”解决了上述问题。这些“Cookie”被发送到客户端,它保存了所有能够辨别用户的信息。我们把这种能够实现相互沟通交界面就叫做Session对象,也就是通常我们所说的会话对象。

The Session object is used to store information about, or change settings for a user session. Variables stored in the Session object hold information about one single user, and are available to all pages in one application. Common information stored in session variables are name, id, and preferences. The server creates a new Session object for each new user, and destroys the Session object when the session expires.
Session对象是用来存储用户的”Session”的,它也可以对用户的Session进行改变和设置。对于Session对象中的变量所存储的单个用户的信息,在同一请求下的所有页面都可以访问到它。一般情况下,存储在Session变量中的信息主要是姓名、id以及一些相关的属性和参数。服务器会为每一个用户建立一个全新的Session对象,当Session过期时,它也会删除这些Session对象。


When does a Session Start?
Session在什么时候开始生效?

A session starts when:
以下列举了Session生效的时刻:

  • A new user requests an ASP file, and the Global.asa file includes a Session_OnStart procedure
    当一个用户请求ASP文件时,“Global.asa”文件中包含了一个“Session_OnStart”程序
  • A value is stored in a Session variable 
    当Session变量当中存有一个值
  • A user requests an ASP file, and the Global.asa file uses the <object> tag to instantiate an object with session scope
    当一个用户请求ASP文件时,“Global.asa”文件使用了“<object>”标签来定义了Session对象的范围 

When does a Session End?
Session在什么时候结束?

A session ends if a user has not requested or refreshed a page in the application for a specified period. By default, this is 20 minutes.
如果用户在特定一段时间内没有重新请求页面或刷新页面,那么,“Sessioin”便结束了。默认情况下,这段时间是20分钟。

If you want to set a timeout interval that is shorter or longer than the default, you can set the Timeout property.
如果你不想采用默认的过期时间,而是想自己定义一个Session过期的时间,你可以采用“Timeout”属性。

The example below sets a timeout interval of 5 minutes:
下面这个例子把“Session“的过期时间设置为5分钟:

<%
Session.Timeout=5
%>

To end a session immediately, you may use the Abandon method:
如果你想直接结束Session,那么你可以使用“abandon”命令:

<%
Session.Abandon
%>

Note: The main problem with sessions is WHEN they should end. We do not know if the user's last request was the final one or not. So we do not know how long we should keep the session "alive". Waiting too long for an idle session uses up resources on the server, but if the session is deleted too soon the user has to start all over again because the server has deleted all the information. Finding the right timeout interval can be difficult!
注意:Session最容易出问题的地方就在于它该什么时候结束。因为我们并不知道用户所谓的最后一个请求是不是真正的最后一个请求,所以我们就很难设定Session该在多少时间内失效。如果让一个已经没有任何用处的Session等待太长的时间会占用很多服务器系统资源;但是,如果把失效的时间设置得很短,那么用户就要时不时的启动Session(因为服务器在Session失效后会删除Session的信息),所以这对用户来说又很不方便。因此,给Session设定一个合理的失效时间是很困难的。

Tip: If you are using session variables, store SMALL amounts of data in them.
提示:往Session变量中存入的信息量应该尽可能的少些。


Store and Retrieve Session Variables
存储以及获取Session变量的值

The most important thing about the Session object is that you can store variables in it.
在Session对象的操作中,最重要的一个环节就是你必须往变量中存入值。

The example below will set the Session variable username to "Donald Duck" and the Session variable age to "50":
下面的例子将把Session的“Username”变量的值设置为“Donald Duck”,把“Age”变量的值设置为“50”:

<%
Session("username")="Donald Duck"

Session("age")=50
%>

When the value is stored in a session variable it can be reached from ANY page in the ASP application:
当这个值被存储在Session变量里时,ASP应用程序下面的所有页面都可以访问这个值。

Welcome <%Response.Write(Session("username"))%>

The line above returns: "Welcome Donald Duck".
上面这个行会返回:"Welcome Donald Duck"

You can also store user preferences in the Session object, and then access that preference to choose what page to return to the user. 

The example below specifies a text-only version of the page if the user has a low screen resolution:
你同样也可以往Session对象当中存储用户的属性,然后通过对这些属性的访问和验证
来选择将哪些特定页面展示给用户,具体案例如下:

<%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 Variables
删除Session变量

The Contents collection contains all session variables.
“Contents”中包含了所有Session的变量集。

It is possible to remove a session variable with the Remove method.
你可以使用“Remove”的方法来删除Session变量。

The example below removes the session variable "sale" if the value of the session variable "age" is lower than 18:
下面的例子展示了如果Session的变量中如果存在着“age”变量,并且其值如果小于18的话,那么,程序将把所有名为“sale”的Session变量全部删除。

<%
If Session.Contents("age")<18 then 
Session.Contents.Remove("sale")
End If 
%>

To remove all variables in a session, use the RemoveAll method:
如果你想把所有的Session变量全部删除,那么你可以使用“RemoveAll()”的方法:

<%
Session.Contents.RemoveAll()
%>

 


Loop Through the Contents Collection
通过循环语句展示Session中“Contents”集合内容

The Contents collection contains all session variables. You can loop through the Contents collection, to see what's stored in it:
“Contents”集合包含了所有的Session变量,你可以通过循环语句来展示所有的Session变量名,具体如下:

<%
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 do not know the number of items in the Contents collection, you can use the Count property:
如果你不知道Session集合中到底有多少个变量,那么你可以使用“Count”的方法来获取它,具体如下:

<%
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
%>

Result:
结果:

Session variables: 2
Donald Duck
50

 


Loop Through the StaticObjects Collection
通过循环语句显示所有的“StaticObjects”集合

You can loop through the StaticObjects collection, to see the values of all objects stored in the Session object:
StaticObjects 集合包含 Session 对象范围中用 <OBJECT> 标记创建的所有对象。该集合可用于确定对象特定属性的值,或用于遍历集合并获取所有对象的全部属性。

<%
dim i
For Each i in Session.StaticObjects
Response.Write(i & "<br />")
Next
%>

评论 (0) All

登陆 | 还没注册?