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.

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。