收 藏 投 稿 繁 体 RSS 
站长吧-中国站长学习与交流的网站
首 页 运 营 学 院 建 站 论 坛
Web master8.net  
 网站运营  
  欢迎在本站发布信息,在线投递稿件请点这里。编辑QQ:4908220,欢迎联系交流。
业界动态 创业故事 推广研究 策划盈利 电子商务 企业平台
  站长工具
SEO查询 Whois查询 Pr查询 域名查询 IP查询 网页编辑器
 建站服务  
  如有建站意向,请尽快联系我们,以便安排时间... 建站服务 QQ4908220 QQ:4908220
作品展示 服务范围 服务流程 服务报价 联系方式 付款方式
文章正文  » 您的当前位置: 首页 >> 学院 >> 程序开发 >> ASP
ASP Session
  来源:互联网 | 时间:2007-07-06 | 浏览:   相关评论 | 报告错误 | 发布文章
【字号: | | 】 【背景色 杏仁黄 秋叶褐 胭脂红 芥末绿 天蓝 雪青 灰 银河白(默认色)

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
%>
master8
  • 上一篇:PHP 阻止SQL注入式攻击
  • 下一篇:五招轻松搞定Windows Vista系统自定义

  • 我要投稿  打印本文  推荐本文  加入收藏  返回顶部  关闭窗口
    搜模板(www.somoban.com) 原创网站模板交易平台
    阿里妈妈再掀疯狂采购风,网站广告位严重告急,急召天下站长
    基于PHP+MySQL的整站、模块、插件开发等或者按需求实现相应功能;
基于各PHP主流建站系统CMS,BBS,BLOG等的模板定制,完全手写代码;
整站数据迁移或备份恢复;网页代码优化、重构;整站常规SEO优化;网站技术支持;
点击了解详情...
    站长论坛
    • 验证码: