Session 对象用于存储关于用户会话(session)的信息,或者更改用户会话(session)的设置。
当您在计算机上操作某个应用程序时,您打开它,做些更改,然后关闭它。这很像一次对话(Session)。计算机知道您是谁。它清楚您在何时打开和关闭应用程序。然而,在因特网上问题出现了:由于 HTTP 地址无法保持状态,Web 服务器并不知道您是谁以及您做了什么。
ASP 通过为每个用户创建一个唯一的 cookie 来解决这个问题。cookie 被传送至用户的计算机上,它含有可识别用户的信息。这种接口被称作 Session 对象。
Session 对象用于存储关于用户会话(session)的信息,或者更改用户会话(session)的设置。
存储于 Session 对象中的变量存储单一用户的信息,并且对于应用程序中的所有页面都是可用的。存储于 session 变量中的公共信息通常是 name、id 和参数。服务器会为每个新的用户创建一个新的 Session,并在 session 失效时撤销掉这个 Session 对象。
Session 对象的集合、属性、方法和事件的描述如下:
集合 | 描述 |
---|---|
Contents | 包含所有通过脚本命令追加到 session 的条目。 |
StaticObjects | 包含了所有使用 HTML 的 <object> 标签追加到 session 的对象。 |
属性 | 描述 |
---|---|
CodePage | 规定显示动态内容时使用的字符集。 |
LCID | 设置或返回指定位置或者地区的一个整数。诸如日期、时间好以及货币的内容会根据位置或者地区来显示。 |
SessionID | 为每个用户返回一个唯一的 id。此 id 由服务器生成。 |
Timeout | 设置或返回应用程序中的 Session 对象的超时时间(分钟)。 |
方法 | 描述 |
---|---|
Abandon | 撤销一个用户的 session。 |
Contents.Remove | 从 Contents 集合删除一个项目。 |
Contents.RemoveAll() | 从 Contents 集合删除所有项目。 |
事件 | 描述 |
---|---|
Session_OnEnd | 当一个会话结束时此事件发生。 |
Session_OnStart | 当一个会话开始时此事件发生。 |
设置并返回 LCID
本例演示 "LCID" 属性。该属性设置并返回一个指示位置或者地区的整数。类似于日期、时间和货币等内容都要根据位置或者地区来显示。
- <!DOCTYPE html>
- <html>
- <body>
- <%
- response.write("<p>")
- response.write("The default LCID for this page is: " & Session.LCID & "<br>")
- response.write("The Date format for the above LCID is: " & date() & "<br>")
- response.write("The Currency format for the above LCID is: " & FormatCurrency(350))
- response.write("</p>")
- Session.LCID=1036
- response.write("<p>")
- response.write("The LCID is now changed to: " & Session.LCID & "<br>")
- response.write("The Date format for the above LCID is: " & date() & "<br>")
- response.write("The Currency format for the above LCID is: " & FormatCurrency(350))
- response.write("</p>")
- Session.LCID = 3079
- response.write("<p>")
- response.write("The LCID is now changed to: " & Session.LCID & "<br>")
- response.write("The Date format for the above LCID is: " & date() & "<br>")
- response.write("The Currency format for the above LCID is: " & FormatCurrency(350))
- response.write("</p>")
- Session.LCID = 2057
- response.write("<p>")
- response.write("The LCID is now changed to: " & Session.LCID & "<br>")
- response.write("The Date format for the above LCID is: " & date() & "<br>")
- response.write("The Currency format for the above LCID is: " & FormatCurrency(350))
- response.write("</p>")
- %>
- </body>
- </html>
- The default LCID for this page is: 1033
- The Date format for the above LCID is: 9/20/2013
- The Currency format for the above LCID is: $350.00
- The LCID is now changed to: 1036
- The Date format for the above LCID is: 20/09/2013
- The Currency format for the above LCID is: 350,00
- The LCID is now changed to: 3079
- The Date format for the above LCID is: 20.09.2013
- The Currency format for the above LCID is: 350,00
- The LCID is now changed to: 2057
- The Date format for the above LCID is: 20/09/2013
- The Currency format for the above LCID is: £350.00
返回 SessionID
本例演示 "SessionID" 属性。该属性为每位用户返回一个唯一的 id。这个 id 由服务器生成。
- <!DOCTYPE html>
- <html>
- <body>
- <%
- Response.Write(Session.SessionID)
- %>
- </body>
- </html>
- 619100248
session 的超时
本例演示 "Timeout" 属性。该属性设置并返回 session 的超时时间(分钟)。
- <!DOCTYPE html>
- <html>
- <body>
- <%
- response.write("<p>")
- response.write("Default Timeout is: " & Session.Timeout & " minutes.")
- response.write("</p>")
- Session.Timeout=30
- response.write("<p>")
- response.write("Timeout is now: " & Session.Timeout & " minutes.")
- response.write("</p>")
- %>
- </body>
- </html>
- Default Timeout is: 20 minutes.
- Timeout is now: 30 minutes.