javascript asp教程第十二课---session对象

Overview:

The Session Object is how you track a single user across many pages. It has four (4) properties, two (2) collections, one (1) method, and two (2) events.

Get Started:

In this series of examples we will create a password system. We will use the Session Object to track whether or not a user is authorized to view certain pages. Below are several scripts for lesson12. Look at them, play with, and then read the explanations that come further down the page.

<%@LANGUAGE="JavaScript"%> <% //No ASP Here, just a regular HTML Page %> <HTML> To play along with our password page, put in a user name and a password.<BR> <BR>The correct user name is <I>guest</I>.<BR> And the correct password is also <I>guest</I>.<BR> <FORM METHOD="post" ACTION="script12a.asp"> User:<INPUT TYPE="text" SIZE="9" NAME="userName" VALUE="guest"><BR> Pass:<INPUT TYPE="password" SIZE="9" NAME="userPassword" VALUE="guest"> <BR> <INPUT TYPE="submit" value="Login"> </FORM> </HTML>

Click Here to run script12.asp in a new window. Below is script12a.asp.

<%@LANGUAGE="JavaScript"%> <% var userName=new String(Request.Form("userName")) var userPassword=new String(Request.Form("userPassword")) if (userName=="guest" && userPassword=="guest") { Session("Authorized")=true Response.Redirect("script12b.asp") } else { Session("Authorized")=false %> <HTML> You did not supply the correct Name & Password.<BR> <A HREF="script12.asp">Click here</A> to log in. </HTML> <% } //end else statement %>

We'll skip over script12b.asp entirely because it's almost exactly the same as script12c.asp. Down below is script12c.asp.

<%@LANGUAGE="JavaScript"%> <% if (Session("Authorized")!=true) { %> <HTML> You are not an authorized user.<BR> <A HREF="script12.asp">Click here</A> to log in. </HTML> <% } else { %> <HTML> The <B>second</B> of two pages that are password protected.<BR> <A HREF="script12d.asp">Click Here</A> to log out. </HTML> <% } //end of else statement %>

Above is script12c.asp, which is the second of two password-protected pages. Below is script12.asp, which is the logout page.

<%@LANGUAGE="JavaScript"%> <% if (Session("Authorized")!=true) { %> <HTML> You are not an authorized user.<BR> <A HREF="script12.asp">Click here</A> to log in. </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="script12b.asp">script12b.asp</A><BR> <A HREF="script12c.asp">script12c.asp</A><BR> <A HREF="script12d.asp">script12d.asp</A><BR> </HTML> <% } //end of else statement %>

A Quick Explanation:

After all that, the last thing you want to see is another grey box full of code. Sorry to do it one more time but, the keystone to this system is in script12a.asp. I've reprinted it down below.

if (userName=="guest" && userPassword=="guest") { Session("Authorized")=true Response.Redirect("script12b.asp") }

Any page can now be turned into a password protected page with the following line: if (Session("Authorized")!=true). Session Variables are part of the Session Collections. Let's talk about them.

Session Collections:

The two Session Collections are Session.Contents and Session.StaticObjects. They parallel the Application.Contents and Application.StaticObjects.

Using Session.Contents("someVariable")="someValue" we can set Session Variables. These variables allow us to carry values from one page to the next. Since Contents is the default collection we can use a little shortcut. It goes like this: Session("someVariable")="someValue".

The shortcut is what you saw in the scripts above.

Session.Contents has two methods of its own. They are Session.Contents.Remove("variableName") and Session.Contents.RemoveAll().

We did not demonstrate StaticObjects in the scripts above. It comes in the form of the <OBJECT> flag (set for Session scope).

No Sharing:

Remember how Application variables could be shared by all viewers on your ASP web site? That's not so with Session Variables. They are private; Session Variables are to be accessed only by one user.

Session.Abandon( ):

My bet is you could figure out Session.Abandon() without any explanation. However, let me just state for the record that Session.Abandon() ends the user's session and releases the Session Variables from memory.

Session Properties:

Let's briefly discuss two properties you are not likely to use. Session.CodePage lets you call for foreign character sets, like Chinese or Russian. Session.LCID is a location identifier. It determines the time zone and language for the server. Don't mess with it.

Now let's move on to something you might actually use from time to time. Session.SessionID is a read-only property generated by the server, and assigned to one specific user during one specific session. The SessionID follows the user from the beginning of the session until the end.

Session.Timeout is the number of minutes that a user can be idle before the Server ends the user's individual session and reclaims the memory that was allocated to Session Variables. (Session.Timeout is demonstrated in the global.asa in lesson 10.)

Session Events:

The two Session events are Session_OnStart() and Session_OnEnd(). These events are accessed in the global.asa. You can see an example of global.asa in lesson 10.

时间: 2024-09-19 09:20:36

javascript asp教程第十二课---session对象的相关文章

javascript asp教程第十二课---session对象_ASP基础

Overview: The Session Object is how you track a single user across many pages. It has four (4) properties, two (2) collections, one (1) method, and two (2) events. Get Started: In this series of examples we will create a password system. We will use

javascript asp教程第十课--global asa_ASP基础

Global.asa: First of all, what is a global.asa? It's an optional script file that holds certain "global" information that you can access through the entire ASP appliciation. The global.asa is a plain text file saved with the .asa extension. You

javascript asp教程第十课--global asa

Global.asa: First of all, what is a global.asa? It's an optional script file that holds certain "global" information that you can access through the entire ASP appliciation. The global.asa is a plain text file saved with the .asa extension. You

数据结构教程 第十二课 实验二 循环链表实验

本课主题: 实验二 循环链表实验 教学目的: 掌握单向链表的实现方法 教学重点: 单向链表的存储表示及操作 教学难点: 单向链表的操作实现 授课内容: 一.单向链表的存储表示 C源程序 #include<stdio.h> #include<malloc.h> #include<conio.h> #define ERROR 0 #define OK 1 #define EQUAL 1 #define OVERFLOW -1 #define LIST_INIT_SIZE 1

数据结构教程 第二十二课 实验五 数组实验

教学目的: 掌握二维数组的实现方法 教学重点: 二维数组的存储表示,二维数组的基本操作 教学难点: 二维数组的基本操作 授课内容: 数组的顺序存储表示和实现: #include<stdarg.h> #define MAX_ARRAY_DIM 8 typedef struct { ElemType *base; int dim; int *bounds; int *constants; }Array; Status InitArray(Array &A,int dim,...); Sta

Asp.Net Web API 2第十二课——Media Formatters媒体格式化器

原文:Asp.Net Web API 2第十二课--Media Formatters媒体格式化器 前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本教程演示如何在ASP.NET Web API中支持额外的媒体格式. Internet Media Types--Internet的媒体类型 媒体类型,也叫做MIME类型,标识了一片数据的格式.在HTTP中,媒体类型描述了消息体

ASP.NET 2.0数据教程之六十二:创建一个用户自定义的Database-Driven Site Map

返回"ASP.NET 2.0数据教程目录" ASP.NET 2.0数据教程之六十二:创建一个用户自定义的Database-Driven Site Map Provider 导言: ASP.NET 2.0的网站地图(site map)功能允许页面开发者在一些 持久介质(persistent medium),比如一个XML文件里,自己定义一个web程序的 site map.一旦定义了之后,我们可以通过System.Web命名空间的SiteMap class 类或某个Web导航控件,比如Si

Senparc.Weixin.MP SDK 微信公众平台开发教程(十二):OAuth2.0说明

原文:Senparc.Weixin.MP SDK 微信公众平台开发教程(十二):OAuth2.0说明 紧接上一篇<Senparc.Weixin.MP SDK 微信公众平台开发教程(十一):高级接口说明>,这里专讲OAuth2.0. 理解OAuth2.0 首先我们通过一张图片来了解一下OAuth2.0的运作模式: 从上图我们可以看到,整个过程进行了2次"握手",最终利用授权的AccessToken进行一系列的请求,相关的过程说明如下: A:由客户端向服务器发出验证请求,请求中

javascript asp教程错误处理_ASP基础

The ASPError Object has zero (0) Methods, nine (9) Properties, zero (0) Events, and zero (0) Collections. AspCode AspDescription Category Column Description File Line Number Source The way you access the ASPError Properties is with a Server Method. Y