ASP Session
Session 的集合与属性、Abandon、Timeout、LCID 与 CodePage。
属性一览
| Member | Type | Purpose |
|---|---|---|
SessionID | 只读 | 唯一标识这个会话 |
Timeout | 读写 | 无活动多少分钟失效 |
CodePage | 读写 | 会话内字符串编码,中文用 65001 |
LCID | 读写 | 日期、货币格式,简体中文 2052 |
Contents | 集合 | 所有 Session("x") |
StaticObjects | 集合 | <object runat=Server> 声明的对象 |
Abandon | 方法 | 丢弃会话 |
CodePage 与 LCID
<%
Session.CodePage = 65001 ' UTF-8
Session.LCID = 2052 ' 简体中文
Response.Write FormatCurrency(1234.5) & "<br>"
Response.Write FormatDateTime(Now(), 1) & "<br>"
Response.Write WeekdayName(Weekday(Now()))
%>¥1,234.50
2026年11月3日
星期二With LCID = 2052, FormatCurrency uses the yuan symbol, FormatDateTime uses Chinese date order and WeekdayName returns Chinese names. The default is English formats.
Timeout
<%
Session.Timeout = 5 ' 5 分钟无活动就失效
Response.Write "剩余时间:" & Session.Timeout & " 分钟"
%>The timeout counts from the last request, not from creation, so an active user never expires. Mobile apps often return after long gaps, and too short a timeout logs users out unexpectedly.
遍历与清理
<%
Dim k
For Each k In Session.Contents
Response.Write k & " = " & Session(k) & "<br>"
Next
Response.Write "共 " & Session.Contents.Count & " 项<br>"
Session.Contents.Remove("cart")
Session.Contents.RemoveAll
%>Abandon 的真实行为
<%
Session("uid") = 42
Session.Abandon
Response.Write Session("uid") ' 仍然输出 42
%>42Abandon only marks the session for disposal; values remain readable on the current page and are actually destroyed after it finishes. Calling Abandon then Redirect is safe, but reading session values later on the same page returns stale data.
ASPSESSIONID cookie. Clearing cookies starts a new session while the online count never decrements, because Session_OnEnd fires only on timeout or Abandon, never on cookie deletion.常见误用
- 把大对象放 Session —— 每个用户一份,几十个用户就把内存吃光
- 在 Session 里存数据库连接 —— 连接数暴涨,且事务状态混乱
- 依赖 Session 做权限 —— 会话劫持后权限全失,需配合 IP 校验或二次验证
属性一览
| 成员 | 类型 | 用途 |
|---|---|---|
| 会话 ID | 只读 | 唯一标识这个会话 |
| 超时 | 读写 | 无活动多少分钟失效 |
| 代码页 | 读写 | 会话内字符串编码,中文用 65001 |
| 区域设置 | 读写 | 日期、货币格式,简体中文 2052 |
| 变量集合 | 集合 | 所有 Session("x") |
| 对象集合 | 集合 | <object runat=Server> 声明的对象 |
| 销毁 | 方法 | 丢弃会话 |
CodePage 与 LCID
<%
Session.CodePage = 65001 ' UTF-8
Session.LCID = 2052 ' 简体中文
Response.Write FormatCurrency(1234.5) & "<br>"
Response.Write FormatDateTime(Now(), 1) & "<br>"
Response.Write WeekdayName(Weekday(Now()))
%>¥1,234.50
2026年11月3日
星期二LCID = 2052 之后,FormatCurrency 用人民币符号,FormatDateTime 用中文日期格式,WeekdayName 返回中文星期。不改的话默认是英文格式。
Timeout
<%
Session.Timeout = 5 ' 5 分钟无活动就失效
Response.Write "剩余时间:" & Session.Timeout & " 分钟"
%>超时从最后一次请求算起,不是从会话创建算起。用户一直在操作就不会失效。移动端应用常有「打开页面很久才回来」的情况,超时设太短会让用户莫名掉线。
遍历与清理
<%
Dim k
For Each k In Session.Contents
Response.Write k & " = " & Session(k) & "<br>"
Next
Response.Write "共 " & Session.Contents.Count & " 项<br>"
Session.Contents.Remove("cart")
Session.Contents.RemoveAll
%>Abandon 的真实行为
<%
Session("uid") = 42
Session.Abandon
Response.Write Session("uid") ' 仍然输出 42
%>42Abandon 只是「标记销毁」,当前页面里仍然能读到值。真正的销毁发生在页面处理完之后。所以登出页 Abandon 之后立刻 Redirect 是安全的,但同一页里继续读会话值会拿到旧数据。
ASPSESSIONID Cookie 里。用户清掉 Cookie 就换了新会话,Application 里的在线人数却减不下来——Session_OnEnd 只在超时或 Abandon 时触发,不会因为 Cookie 被删而触发。常见误用
- 把大对象放 Session —— 每个用户一份,几十个用户就把内存吃光
- 在 Session 里存数据库连接 —— 连接数暴涨,且事务状态混乱
- 依赖 Session 做权限 —— 会话劫持后权限全失,需配合 IP 校验或二次验证