[设为首页] [加入收藏]    
提 示: 站长书库上线了。 站长招聘上人才沃
站长首页  |  站长下载  |  站长工具
站长素材  |  广告代码  |  网页特效
站长书库  |  站长招聘  |  建站软件
  您当前的位置: 首页 > 网络编程 > ASP教程 >
Session

我们在访问一个论坛的期间,叫做会话期间,在这个期间产生的各种信息叫做会话信息,Session就是用来处理会话信息的一个对象,在会话期间Session可以将变量,对象和数组保存到客户的Cookies中,所以如果客户禁用了Cookies,则Session的会话功能将不能使用,Session的使用格式很简单,如下:

一 Session的基本应用

Session("变量名")=赋值表达式

1 Session对象有两个属性:

(1)Session.SessionID 存贮用户的SessionID,在会话期间服务器会为每一个客户生成一个单独的标记即SessionID,它以长整形数据类型返回,可以用于 Web 页面的注册统计;

(2)Session.TimeOut 设置Session的有效时长,一个Session对象从保存到失效默认是20分钟,如果客户长时间不刷新页面或提交请求,Session对象就会自动结束,TimeOut属性可以设置Session对象的有效时长,格式为:Session.TimeOut=整数(分);

2 Session对象有一个方法:

Session.Abandon 用于清除Session对象,

3 Session对象有两个事件;

(1)Session_OnStart 在Session对象开始之前调用这个程序

(2)Session_OnEnd 在Session对象结束之后调用这个程序

示例sl9-1 写入与取出Session

(1)sl9-1-1.asp

<html>
<head><title>写入Session</title></head>
<body>
<%
response.write "写入Session"
response.write "<br>"
Session("your_name")="小赵"
response.write "<br>"
Session("your_age")=20
%>
</body>
</html>

(2) sl9-1-2.asp

<html>
<head><title>取出Session </title></head>
<body>
<%
response.write "取出Session"
response.write "<br>"
response.write(Session("your_name"))
response.write "<br>"
response.write(Session("your_age"))
%>
</body>
</html>

示例sl9-2 使用表单写入Session

(1) sl9-2-1.asp

创建一个表单并将表单的信息发送到sl9-2-2.asp文件中

<html>
<body>
<form action="sl9-2-2.asp" method=post >
用户姓名:
<input type="text" name="myname" size="19"><br>
用户密码:
<input type="password" name="mypassword">
<input type="submit" value="提交">
</form>
</body>
</html>

(2) sl9-2-2.asp

<html>
<body>
<%
'获取表单信息,保存到Session中
Session("myname")=Request.form("myname")
Session("mypassword")=Request.form("mypassword")

response.write("你输入的姓名:"&Session("myname"))
response.write "<br>"
response.write "你输入的密码:"&Session("mypassword")

%>
</body>
</html>

示例解读:

点击sl9-2-1.asp中的提交按钮,则客户在表单中输入的信息发送给sl9-2-2.asp文件并将其打开,sl9-2-2.asp文件完成了如下功能,使用Request.form("my_text1")接收表单中的信息,并将其保存到Session("kk")对象的变量kk中,客户首次来访时创建kk,再次来访时重新赋值,最后显示变量kk的信息。

示例 sl9-3.asp

<html>
<head><title>asp简单程序</title></head>
<body>
<%
'获取SessionID并输出显示
response.write("你的Session.SessionID为:"&Session.SessionID)
response.write "<br>"
'获取Session的默认时长并输出显示
response.write("你的Session.Timeout时长为:"&Session.Timeout)
%>
<!-- 构造表单提供改变Session时长的输入框 -->
<form action="" method=post >
设置时长为:
<input type="text" name="m_text" size="19" value="30">
<br>
<input type="Hidden" name="Hidden_name" value="688">
<input type="submit" name="mytype" value="设置">
</form>

<%
dim m,Hidden_name
'获取对Session时长设置的数据
m=Request.form("m_text")
if m<>"" then
Session.Timeout=cint(m)
response.write "<br>"
'获取Session的时长并输出显示
response.write("Session.Timeout时长设置改为:"&Session.Timeout)
end if
%>
</body>
</html>

示例sl9-4

(1) sl9-4-1.asp

<html>
<head><title>asp简单程序</title></head>
<body>
<%
if Session("ss_name")<>"" then
response.write("热烈欢迎"&Session("ss_name")&"的到来")
else
%>
<form action="sl9-4-2.asp" method=post %>
用户姓名:
<input type="text" name="my_text1" size="19"><br>
用户密码:
<input type="password" name="password">
<input type="submit" name="mytype" value="提交">
</form>
<%
end if
%>
</body>
</html>

示例解读:

程序使用条件语句判断客户是否已经在Cookies中保存了姓名,如果Session("ss_name")不为空值,说明已经提交了姓名,则输出欢迎信息,否则就构造一个表单,供客户在文本框输入姓名并提交;

(2) sl9-4-2.asp

<html>
<body>
<%
Session("ss_name")=Request.form("my_text1")
Session("password")=Request.form("password")
if Session("ss_name")<>"" and Session("password")<>"" then
response.write("注册成功,欢迎"&Session("ss_name")&"的到来")
else
response.write("你需要提交姓名和密码!")
end if
%>
</p>
<p><a href="sl9-4-1.asp">返回
</a></p>
</body>
</html>

示例解读:

本页接收sl9-4-1.asp表单中的信息,并把其保存到Cookies中,使用条件语句判断Session的值,输出相应的语句

示例 sl9-5.asp

<html>
<body>
<%
dim my_array(3) '定义数组
my_array(0)="小李" '为数组元素赋值
my_array(1)="小王"
my_array(2)="小红"
my_array(3)="小黄"
Session("m_y_array")=my_array '将数组保存到Session对象中
response.write Session("m_y_array")(0)
response.write "<br>"
response.write Session("m_y_array")(1)
response.write "<br>"
response.write Session("m_y_array")(2)
response.write "<br>"
response.write Session("m_y_array")(3)
response.write "<br>"
dim sum
'使用循环语句枚举集合中的元素
for each sum in Session("m_y_array")
response.write(sum&",")
next
%>
</body>
</html>

    关于我们 - 联系我们 - 广告服务 - 网站地图 - 版权声明 - 人才招聘