能够运用 Session 对象存储特定用户会话所需的信息。如许,当用户在应用程序的 Web 页之间跳转时,存储在 Session 对象中的变量将不会丧失,而是在悉数用户会话中一向存在下去。
当用户要求来自应用程序的 Web 页时,假如该用户还没有会话,则 Web 服务器将自动建立一个 Session 对象。当会话逾期或被摒弃后,服务器将停止该会话。
Session 对象最常见的一个用法就是存储用户的首选项。比方,假如用户指明不喜欢检察图形,就能够将该信息存储在 Session 对象中。有关运用 Session 对象的细致信息,请参阅“ASP 应用程序”部份的“治理会话”。
注重 会话状况仅在支撑 cookie 的浏览器中保存。
语法
Session.collection|property|method
鸠合
Contents 包括已用剧本敕令添加到会话中的项目。
StaticObjects 包括经由过程 <OBJECT> 标记建立的并给定了会话作用域的对象。
属性
CodePage 将用于标记映照的代码页。
LCID 现场标识。
SessionID 返回用户的会话考证。
Timeout 应用程序会话状况的超时时限,以分钟为单元。
要领
Abandon 该要领损坏 Session 对象并开释其资本。
事宜
global.asa 文件中声明以下事宜的剧本。
Session_OnEnd
Session_OnStart
有关以上事宜及 global.asa 文件的细致信息, 请参阅 Global.asa 参考.
解释
您能够在 Session 对象中存储值。存储在 Session 对象中的信息在会话及会话作用域内有效。以下剧本演示两种范例的变量的存储体式格局。
<% 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 文件中运用 <OBJECT> 标记。
然则不能在 Session 对象中存储内建对象。比方,下面每一行都将返回毛病。
<% 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("<script>alert('请上岸!');window.history.go(-1);</scritp>") end if
【相干引荐】
1. ASP免费视频教程
2. 关于ASP中session的细致引见
3. 教你处理ASP session丧失的要领
4. 引见ASP中Session对象的三种要领
5. 详解ASP中Session的运用技能
以上就是ASP session简朴示例的细致内容,更多请关注ki4网别的相干文章!