ASP中session如何使用,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

目前创新互联已为1000多家的企业提供了网站建设、域名、网站空间、网站运营、企业网站设计、秀洲网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
Session 对象
可以使用 Session 对象存储特定用户会话所需的信息。这样,当用户在应用程序的 Web 页之间跳转时,存储在 Session 对象中的变量将不会丢失,而是在整个用户会话中一直存在下去。
当用户请求来自应用程序的 Web 页时,如果该用户还没有会话,则 Web 服务器将自动创建一个 Session 对象。当会话过期或被放弃后,服务器将终止该会话。
Session 对象最常见的一个用法就是存储用户的首选项。例如,如果用户指明不喜欢查看图形,就可以将该信息存储在 Session 对象中。有关使用 Session 对象的详细信息,请参阅“ASP 应用程序”部分的“管理会话”。
注意 会话状态仅在支持 cookie 的浏览器中保留。
语法
Session.collection|property|method
集合
Contents 包含已用脚本命令添加到会话中的项目。
StaticObjects 包含通过
<%
Session("username") = "Janine"
Session("age") = 24
%>但是,如果您将对象存储在 Session对象中,而且您使用 VBScript 作为主脚本语言。则必须使用关键字 Set。如下列脚本所示。
<% Set Session("Obj1") = Server.CreateObject("MyComponent.class1") %>然后,您就可以在后面的 Web 页上调用 MyComponent.class1 揭示的方法和属性,其调用方法如下:
<% Session("Obj1").MyMethod %>也可以通过展开该对象的本地副本并使用下列脚本来调用:
<%
Set MyLocalObj1 = Session("Obj1")
MyLocalObj1.MyObjMethod
%>创建有会话作用域的对象的另一种方法是在 global.asa 文件中使用
<%
Set Session("var1") = Session
Set Session("var2") = Request
Set Session("var3") = Response
Set Session("var4") = Server
Set Session("var5") = Application
%>在将对象存储到 Session 对象之前,必须了解它使用的是哪一种线程模型。只有那些标记为“Both”的对象才能存储在没有锁定单线程会话的 Session 对象中。详细信息, 请参阅“创建 ASP 组件”中的“选择线程模型”。
若您将一个数组存储在 Session对象中,请不要直接更改存储在数组中的元素。例如,下列的脚本无法运行。
<% Session("StoredArray")(3) = "new value" %>
这是因为 Session对象是作为集合被实现的。数组元素 StoredArray(3) 未获得新的赋值。而此值将包含在 Application 对象集合中,并将覆盖此位置以前存储的任何信息。
我们极力建议您在将数组存储在 Session对象中时,在检索或改变数组中的对象前获取数组的一个副本。在对数组操作时,您应再将数组全部存储在 Session 对象中,这样您所做的任何改动将被存储下来。下列的脚本对此进行演示。
---file1.asp---
<%
'Creating and initializing the array
Dim MyArray()
Redim MyArray(5)
MyArray(0) = "hello"
MyArray(1) = "some other
string
"
'Storing the array in the Session object
Session("StoredArray") = MyArray
Response.Re
dir
ect("file2.asp")
%>
---file2.asp---
<%
'Retrieving the array from the Session Object
'and mod
if
ying its second element
LocalArray = Session("StoredArray")
LocalArray(1) = " there"
'
print
ing out the string "hello there"
Response.Write(LocalArray(0)&LocalArray(1))
'Re-storing the array in the Session object
'This overwrites the values in StoredArray with the new values
Session("StoredArray") = LocalArray
%>相关解答:
A页面需输入正确的用户名和密码后,
加条语句:session("isLogin")=true
在B页面前判断:
if session("isLogin")=false then
response.write "未登录"
response.en
end if这样就可以了
登陆后设置:
session("user")=用户名
在B页面输入:
if session("user")="" then
Response.write("

咨询
建站咨询