lecture note 7: sessions and applications object

26
Lecture Note 7: Sessions and Applications Object

Upload: harvey-hardy

Post on 17-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture Note 7: Sessions and Applications Object

Lecture Note 7: Sessions and Applications Object

Page 2: Lecture Note 7: Sessions and Applications Object

What are Sessions?

Sessions are a very convenient ASP feature. When someone visits a web page on your site, ASP calls that a "session" and immediately can differentiate that user from all other users at a site. Anything stored in that user's session can be retrieved and manipulated from that page and the next pages they visit, and the data will be tied to that user. Session data is generally attached to one user. When a user visits his first page of your site, that page and every page he visited is collectively called a session. Any data attached or stored in that session object is private to the pages that the user is visiting.

The code to store data in a session variable is simple.  Here we will allow a user to flip a coin, i.e. flipcoin.asp and count their successes:

<Test Script Below –Click to test >

Page 3: Lecture Note 7: Sessions and Applications Object

<%

response.write "Coin Tossed!<br>"

randomize

randomnum=int(rnd*2)+1

IF randomnum=1 THEN

session("heads")=session("heads")+1

ELSE

session("tails")=session("tails")+1

END IF

response.write "Heads= " & session("heads") & "<br>"

response.write "Tails= " & session("tails") & "<br>"

%>

When you run this asp program, it will show at the first time:

Coin Tossed!Heads= Tails= 1

Page 4: Lecture Note 7: Sessions and Applications Object

When you refresh this asp program (means run it again), it will show :

Coin Tossed!Heads=1 Tails= 1

Test it again:

Coin Tossed!Heads=2 Tails= 1

again…..

Coin Tossed!Heads=5 Tails= 3…..

Even though there are many people at the site they all have different scores for their "heads" and "tails" count. They each has a session and it co-ordinates and differentiates their values.

Page 5: Lecture Note 7: Sessions and Applications Object

ASP Session ObjectASP Session Object

The Session object

The Session object is used to store information about, or change settings for a user session.

Variables stored in the Session object hold information about one single user, and are available to all pages in one application.

Common information stored in session variables are name, id, and preferences.

The server creates a new Session object for each new user, and destroys the Session object when the session expires.

Page 6: Lecture Note 7: Sessions and Applications Object

ASP Session ObjectASP Session Object

When does a Session Start?

A session starts when:

A new user requests an ASP file, and the Global.asa file includes a Session_OnStart procedure

A value is stored in a Session variable

A user requests an ASP file, and the Global.asa file uses the <object> tag to instantiate an object with session scope

Page 7: Lecture Note 7: Sessions and Applications Object

ASP Session ObjectASP Session Object

When does a Session End?

A session ends if a user has not requested or refreshed a page in the application for a specified period.By default, this is 20 minutes.

You also can set the Timeout property.The example below sets a timeout interval of 5 minutes:

<%Session.Timeout=5%>

To end a session immediately, you may use the Abandon method:

<%Session.Abandon%>

Page 8: Lecture Note 7: Sessions and Applications Object

ASP Session ObjectASP Session Object

Store and Retrieve Session Variables

Example:

<%Session("username")="Tony Wong“Session("age")=25%>

When the value is stored in a session variable it can be reached from ANY page in the ASP application:

Hello!! <%Response.Write(Session("username"))%>

The line above returns: "Hello!! Tony Wong".

Page 9: Lecture Note 7: Sessions and Applications Object

ASP Session ObjectASP Session Object

Remove Session Variables

The Contents collection contains all session variables. It is possible to remove a session variable with the Remove method.

To remove all variables in a session, use the RemoveAll method: <%Session.Contents.RemoveAll()%>

Page 10: Lecture Note 7: Sessions and Applications Object

ASP Session ObjectASP Session Object

Loop Through the Contents Collection

Example to Show the Contents

<%Session("username")="Tony Wong“Session("age")=25dim IFor Each i in Session.Contents Response.Write(i & "<br />")Next%>

Result: username age

Page 11: Lecture Note 7: Sessions and Applications Object

ASP Session ObjectASP Session Object

Example to Count Contents collection:

<%dim I, jj=Session.Contents.CountResponse.Write("Session variables No: " & j)For i=1 to j Response.Write(Session.Contents(i) & "<br />")Next%>

Result: Session variables No: 2 Tony Wong 25

Page 12: Lecture Note 7: Sessions and Applications Object

ASP Session ObjectASP Session Object

Loop Through the StaticObjects Collection

Loop through the StaticObjects collection to see the values of all objects stored in the Session object:

<%dim IFor Each i in Session.StaticObjects Response.Write(i & "<br />")Next%>

Page 13: Lecture Note 7: Sessions and Applications Object

SessionSession 陣列的索引值陣列的索引值範例;範例;

<%<%************c12_4_1.asp***************c12_4_1.asp***

****************Dim A(5)Dim A(5)For i= 0 to 5For i= 0 to 5 A (i) = iA (i) = iNextNextSession(‘’A’’)=ASession(‘’A’’)=AResponse.Write Session(‘’A’’)Response.Write Session(‘’A’’)

(3)(3)%>%>

若單獨改變若單獨改變 SessionSession 陣列值:陣列值:

Session(‘’A’’)(3)=50 Session(‘’A’’)(3)=50 錯誤錯誤 正確作法正確作法應先將應先將 SessionSession

陣列指定給一般陣列,然陣列指定給一般陣列,然後改變一般陣列值,再將後改變一般陣列值,再將一般陣列指定給一般陣列指定給 SessionSession陣列陣列

例如;例如; temp = Session(‘’A’’)temp = Session(‘’A’’)Temp (3) =50Temp (3) =50Session(‘’A’’) = tempSession(‘’A’’) = temp

Page 14: Lecture Note 7: Sessions and Applications Object

SessionSession 陣列的索引值陣列的索引值<%<%****************c12_4_2.asp*******************************c12_4_2.asp***************Dim A(5)Dim A(5)For i = 0 To 5For i = 0 To 5 A(i) = iA(i) = iNextNextSession(‘’A’’) = ASession(‘’A’’) = AResponse.Write’’Response.Write’’ 舊舊 SessionSession 陣列的值;’’陣列的值;’’ & Session(‘’A’’)& Session(‘’A’’)

(3) & ‘’<p>’’(3) & ‘’<p>’’temp = Session(‘’A’’)temp = Session(‘’A’’)temp(3)=50temp(3)=50Session(‘’A’’) = tempSession(‘’A’’) = tempResponse.Write’’Response.Write’’ 新新 SessionSession 陣列的值;’’陣列的值;’’ & Session(‘’A’’)& Session(‘’A’’)

(3)(3)%>%>

Page 15: Lecture Note 7: Sessions and Applications Object

What is Application data?

which is attached to the web server and is the same no matter which user is accessing the site. Application values are visible to every user. Unlike session data, any web page could change the application's data whenever there is a potential concurrency issued. The lock and unlock method of the application object eliminate concurrency issues. Once an application is locked, no other updates to the application object can occur until the unlock is executed.

One use for an application variable would be to store variables most scripts on a site needed to access.

An example I used to illustrate the conceptual use for each type of data would be a website that simulated a casino. Player's individual winnings make perfect sense to maintain in session variables. However, the total number of players at each "virtual table" (blackjack, roulette, etc.) would be application data which is regardless of an individual player's status.

Page 16: Lecture Note 7: Sessions and Applications Object

What is Application data?

Here is a file called appblackjackarrive.asp that could be included in any script where someone arrived at the blackjack table!

<Test Script Below - Arrive>

<% response.write "Welcome to the BlackJack Table<br>"application.lockapplication("bjplayers")=application("bjplayers")+1application.unlockresponse.write "There are " & application("bjplayers") & " players here!<br>"%>

Page 17: Lecture Note 7: Sessions and Applications Object

What is Application data?

Here is a file called appblackjackleave.asp that could be included in any script where someone left the blackjack table!

<Test Script Below-Leave>

<% response.write "Thanks for playing BlackJack!<br>"application.lockapplication("bjplayers")=application("bjplayers")-1IF application("bjplayers")<0 THEN application("bjplayers")=0END IFapplication.unlockresponse.write "There are " & application("bjplayers") & " players still at the table!<br>"%>

Page 18: Lecture Note 7: Sessions and Applications Object

ASP Application Object ASP Application Object

Lock and Unlock

"Lock" method: When an application is locked, the users cannot change the Application variables (other than the one currently accessing it).

"Unlock" method: This method removes the lock from the Application variable.

<%Application.Lock ………………………………………………Application.Unlock%>

Page 19: Lecture Note 7: Sessions and Applications Object

What is Application data?

Here is a file called appblackjacklook.asp that displays how many people are at the table.

<Test Script Below - Total>

<% response.write "Over at the BlackJack Table<br>"response.write "There are " & application("bjplayers") & " players there!<br>"%>

Page 20: Lecture Note 7: Sessions and Applications Object

ASP Application Object ASP Application Object

Application Object

An application on the Web may be a group of ASP files. The ASP files work together to perform some purpose. The Application object in ASP is used to tie these files together.

The Application object is used to store and access variables from any page.

ALL users share one Application object.

The Application object should hold information that will be used by many pages in the application (like database connection information).

If you change the information in one place and the changes will automatically be reflected on all pages.

Page 21: Lecture Note 7: Sessions and Applications Object

ApplicationApplication 陣列陣列<%<%****************c12_5_4.asp*******************************c12_5_4.asp***************Dim no(5)Dim no(5)For i = 0 To 5For i = 0 To 5 no(i) = ino(i) = iNextNextApplication(‘’no’’) = noApplication(‘’no’’) = noResponse.Write’’Response.Write’’ 舊舊 ApplicationApplication 陣列的值;’’陣列的值;’’ & &

Application(‘’no’’)(3) & ‘’<p>’’Application(‘’no’’)(3) & ‘’<p>’’temp = Application(‘’no’’)temp = Application(‘’no’’)temp(3)=50temp(3)=50Application(‘’no’’) = tempApplication(‘’no’’) = tempResponse.Write’’Response.Write’’ 新新 ApplicationApplication 陣列的值;’’陣列的值;’’ & &

Application(‘’no’’)(3)Application(‘’no’’)(3)%>%>

Page 22: Lecture Note 7: Sessions and Applications Object

ASP Application Object ASP Application Object

Store and Retrieve Application Variables Application variables can be accessed and changed by any page in the application

Example

To create two Application variables: "vartime" and "users" in "Global.asa":

<script language="vbscript" runat="server"> Sub Application_OnStart application("vartime")="“ application("users")=1End Sub </script>

Page 23: Lecture Note 7: Sessions and Applications Object

ASP Application Object ASP Application Object

To access the value of an Application variable:Application variables can be accessed and changed by any page in the application

Example

There are <%Response.Write(Application("users"))%> active connections.

Page 24: Lecture Note 7: Sessions and Applications Object

ASP Application Object ASP Application Object

Loop Through the Contents Collection

Example to Show the Contents:

<%dim IFor Each i in Application.Contents Response.Write(i & "<br />")Next%>

Page 25: Lecture Note 7: Sessions and Applications Object

ASP Application Object ASP Application Object

Loop Through the Contents Collection

Example to Count Contents collection:

<%dim Idim jj=Application.Contents.CountFor i=1 to j Response.Write(Application.Contents(i) & "<br />")Next

Page 26: Lecture Note 7: Sessions and Applications Object

ASP Application Object ASP Application Object

Loop Through the StaticObjects Collection

Loop through the StaticObjects collection, to see the values of all objects stored in the Application object:

<%dim IFor Each i in Application.StaticObjects Response.Write(i & "<br />")Next%>