When you run this asp program, it will show at the first time: Coin Tossed! Heads= Tails= 1"> When you run this asp program, it will show at the first time: Coin Tossed! Heads= Tails= 1">
Download presentation
Presentation is loading. Please wait.
Published byHarvey Hardy Modified over 9 years ago
1
Lecture Note 7: Sessions and Applications Object
2
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
3
<% response.write "Coin Tossed! " 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") & " " response.write "Tails= " & session("tails") & " " %> When you run this asp program, it will show at the first time: Coin Tossed! Heads= Tails= 1
4
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.
5
ASP Session Object ASP 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.
6
ASP Session Object ASP 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 tag to instantiate an object with session scope
7
ASP Session Object ASP 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: To end a session immediately, you may use the Abandon method:
8
ASP Session Object ASP Session Object Store and Retrieve Session Variables Example: When the value is stored in a session variable it can be reached from ANY page in the ASP application: Hello!! The line above returns: "Hello!! Tony Wong".
9
ASP Session Object ASP 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:
10
ASP Session Object ASP Session Object Loop Through the Contents Collection Example to Show the Contents ") Next %> Result: username age
11
ASP Session Object ASP Session Object Example to Count Contents collection: ") Next %> Result: Session variables No: 2 Tony Wong 25
12
ASP Session Object ASP Session Object Loop Through the StaticObjects Collection Loop through the StaticObjects collection to see the values of all objects stored in the Session object: ") Next %>
13
Session 陣列的索引值 範例;<% ************c12_4_1.asp*** ******** Dim A(5) For i= 0 to 5 A (i) = i A (i) = iNextSession(‘’A’’)=A Response.Write Session(‘’A’’)(3) %> 若單獨改變 Session 陣列值: Session(‘’A’’)(3)=50 錯誤 正確作法應先將 Session 陣 列指定給一般陣列,然後 改變一般陣列值,再將一 般陣列指定給 Session 陣列 例如; temp = Session(‘’A’’) Temp (3) =50 Session(‘’A’’) = temp
14
Session 陣列的索引值 <%****************c12_4_2.asp*************** Dim A(5) For i = 0 To 5 A(i) = i A(i) = iNext Session(‘’A’’) = A Response.Write’’ 舊 Session 陣列的值; ’’& Session(‘’A’’)(3) & ‘’ ’’ temp = Session(‘’A’’) temp(3)=50 Session(‘’A’’) = temp Response.Write’’ 新 Session 陣列的值; ’’& Session(‘’A’’)(3) %>
15
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.
16
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 <% response.write "Welcome to the BlackJack Table " application.lock application("bjplayers")=application("bjplayers")+1 application.unlock response.write "There are " & application("bjplayers") & " players here! " %>
17
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 <% response.write "Thanks for playing BlackJack! " application.lock application("bjplayers")=application("bjplayers")-1 IF application("bjplayers")<0 THEN application("bjplayers")=0 END IF application.unlock response.write "There are " & application("bjplayers") & " players still at the table! " %>
18
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.
19
What is Application data? Here is a file called appblackjacklook.asp that displays how many people are at the table. Test Script Below <% response.write "Over at the BlackJack Table " response.write "There are " & application("bjplayers") & " players there! " %>
20
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.
21
Application 陣列 <%****************c12_5_4.asp*************** Dim no(5) For i = 0 To 5 no(i) = i no(i) = iNext Application(‘’no’’) = no Response.Write’’ 舊 Application 陣列的值; ’’& Application(‘’no’’)(3) & ‘’ ’’ temp = Application(‘’no’’) temp(3)=50 Application(‘’no’’) = temp Response.Write’’ 新 Application 陣列的值; ’’& Application(‘’no’’)(3) %>
22
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": Sub Application_OnStart application("vartime")="“ application("users")=1 End Sub
23
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 active connections.
24
ASP Application Object ASP Application Object Loop Through the Contents Collection Example to Show the Contents: ") Next %>
25
ASP Application Object ASP Application Object Loop Through the Contents Collection Example to Count Contents collection: ") Next
26
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: ") Next %>
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.