ASP学习笔记(11)--Session对象

    Session对象是表示如何在不同页面之间跟踪一个客户的有效性。共有4个属性(CodePage  LCID  Timeout  SessionID),两个集合(Contents  StaticObject),一个方法(Abandon()),2个事件(Session_OnStart    Session_OnEnd)。

     CodePage和LCID都是比较少可能会用到的属性。CodePage允许引用外来的字符集,例如Russia等,而LCID=location identifer,定义了server端的时区以及语言。 Timeout就是超时,单位为分钟,超过这个时间之后,server端将不再保存本用户的Session并且回收本Session的变量占用的内存,也就是说本用户不再是一个authorized的了。而SessionID是一个server端生成的唯一的只读的值,定义一个用户的一个session,在本session中,跟用户绑定。

    Session.Contents("variableName")="variableValue"设定session的变量值,此值作为本Session的判断条件。StaticObject是用在asa文件中的。

    Abandon(),很显然的意思,中止此Session,释放Session变量。

    两个时间同样用在global.asa文件中。

    需要注意的一点是Application变量是各用户共享的,而Session变量是用户自己独有的。

例子:

test16.asp

<%@ language=javascript %>
<html>
 <strong><i>an example for session</i></strong><br>
 <strong>the right username is guest </strong><br>
 <strong>the right password is guest </strong><br>
 <form action=fortest16.asp method=post>
  username:<input type=text name=username value=guest><br>
  password:<input type=password name=password value=guest><br>
  <input type=submit value=login>
 </form> 
</html>

fortest16.asp

<%@ language=javascript %>
<%
 var username=new String(Request.Form("username"));
 var password=new String(Request.Form("password"));
 if(username=="guest" && password=="guest"){
  Session.Contents("right")=true;
  Response.Redirect("fortest16_1.asp");
 }
 else {
  Session.Connents("right")=false;
%>
<html>
 <strong>you didnot input the right username and password</strong><br>
 <a href=test16.asp>click here</a >to login
</html>
<%
 }
%>

fortest16_1.asp

<%@ language=javascript %>
<%
 if(Session.Contents("right")!=true){
%>
<html>
 you are not a authorized user<br>
 <a href=test16.asp>click here</a> to login
</html>
<%
 }
 else {
%>
<html>
 this is the first password protected page<br>
 <a href=fortest16_2.asp>click here </a> to enter the second page
</html>
<%
 }
%>

fortest16_2.asp

<%@ language=javascript %>
<%
 if(Session.Contents("right")!=true){
%>
<html>
 you are not a authorized user<br>
 <a href=test16.asp>click here</a> to login
</html>
<%
 }
 else {
%>
<html>
 this is the second password protected page<br>
 <a href=fortest16_3.asp>click here </a> to logout
</html>
<%
 }
%>

fortest16_3.asp

<%@ language=javascript %>
<%
 if(Session.Contents("right")!=true){
%>
<html>
 you are not a authorized user<br>
 <a href=test16.asp>click here</a> to login
</html>
<%
 }
 else {
  var sessionid= Session.SessionID;
  Session.Abandon();
%>
<HTML>
You have sucessfully logged out.<BR>
This was session <%=sessionid%>.<BR><BR>
Now try a link to one of the
pages you've already visited.<BR><BR>
<A HREF="fortest16_1.asp">fortest16_1.asp</A><BR>
<A HREF="fortest16_2.asp">fortest16_2.asp</A><BR>
<A HREF="fortest16.asp">fortest16.asp</A><BR>
</HTML>
<%
 } //end of else statement
%>

Google
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值