javascript asp教程第12課---session対象


Overview:
The Session Object is how you troc a single user acros many pages.It has four(4)properties,two(2)collection s,one(1)method,and two(2)events.
Get Startd:
In this series of examples we will create a password system.We will use the Session Object to track Object to track wher or not a user is authorized to view.Below are scripts.the the the the the the the atlath
Click Here to run script 12 asp in a new window.Below is script 12 a.asp.
<%@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>
We'll skyp over script 12 b.asp entirely because it's almost exactly the same as script 12 c.asp.Down below is script 12 c.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
%>

Above is script 12 c.asp,which is the second of tword protected pages.Below is script 12 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 
	{
%>
<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
%>
A Quick Explanion:
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 in script 12 a.asp.I've presd it down below.
<%@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
%>
Any page can now be turned into a password protected page with the followwing line:if(Session)!==true).Session Varables are part of the Session Collection.Let's talk about them.
Session Collection:
The two Session Collection are Session.Collection and Session.Station.Static Objecs.The y parallel the Aplication.C.Intens and Apple.Station.Static Object.
Using Session.contens(「someVarable」)=「someValue」we can set Session Varables.The variables allow us to carry values from one page to the next.Since Cont is the default collection we protion wen cantine Intre.ifile.ible.ore。
Theショートカットis what you saw in the scripts above.
Session.contenshas two methods of its own.The e e are Session.Contintens.Remove(「variable Name」)and Session.C.ontens.RemoveAll().
We did not demont strate Static Object in the scripts above.It coes in the form of the<OBJECT>flags(set for Session scope).
No Sharing:
Remember how Apple variables could be shared by all view on your ASP web site?That's not so with Session Varables.Thy are prvate;Session Varables are to be accessed only by one user.
Session.Aband():
My bet is You could figure out Session.Aband()without any explanion.However,let me just state for the recorded that Session.Aband()ends the user's session and releass the Session.
Session Propties:
Let's brienfly discuss two properties you are not likely to use.Session.codePage lets you call foreign character sets,like Chinese or Russian.Session.LCID is a location identifer.It determines the forine and
Now let's move on to something you might actually use from time to time.session.Session.Session ID is a read-only propertygenerand by the server,and assigned to one specifeng specifinic session.The thersionight the
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 memorythat was allocated to Session Vareballes.
Session Events:
The two Session events are Session_OnStart()and Session_OnEnd().The events are accessed in the global.asa.You can see an example of global.asa in lesson 10.