Presentation is loading. Please wait.

Presentation is loading. Please wait.

Maintaining State in ASP. Problem - How do I maintain state information about the user  Several Methods –Cookies –Session variables –Hidden fields 

Similar presentations


Presentation on theme: "Maintaining State in ASP. Problem - How do I maintain state information about the user  Several Methods –Cookies –Session variables –Hidden fields "— Presentation transcript:

1 Maintaining State in ASP

2 Problem - How do I maintain state information about the user  Several Methods –Cookies –Session variables –Hidden fields  Each have advantages and disadvantages

3 Cookies  You can use Cookies to hold information about a user on their computer. Cookies contain a string of Variable = Value items that can be set and queried by the Browser or at the server (Server side cookies)  If the Expire date/time of a cookie is earlier than the current time, the cookie is deleted at the end of the session, otherwise the values can be retrieved later.

4 Setting Cookie Values  <% Response.Cookies(“CookieName”).Expires =#Date# (I.e.. May 5, 2004) –If you don’t set the expires date, it defaults to the end of the session.  Response.Cookies(“CookieName”)(“variable”)=“t he value string” %>  EG. –Response.Cookies(“MusicApp”)(“user”)=Request.Quer yString(“UserName”) Will set the variable user in the MusicApp cookie to the value in the UserName field of the form

5 State maintenance  So, you can use session cookies to see if a user has authenticated, and to track info about them from page to page.  When the user shuts the browser down, the info is lost.

6

7 Session Object  <% Session("username")="Donald Duck"  Session("age")=50 %>  Welcome

8 Session Objects are maintained at the Server  This means they do not have to be sent back to the browser  Session Objects can have timeout limits, default is 20 minutes  –Will rest timeout to 5 minutes, I.e. if user does not do anything for 5 minutes the session info is dumped…  –Closes Session Immediately

9 Application Object Holds information about the Application  Sub Application_OnStart application("vartime")="" application("users")=1 End Sub  There are active connections.  You can Lock and Unlock the Application Object –<% Application.Lock –‘do some application object operations –Application.Unlock %>

10 Hidden Fields  You can pass info from one page to another  By using a hidden form field –This method gets messy if you have lots of pages, in that case, better to use cookies or session variables

11 Other tricks  Response.Redirect –Use Response.Redirect to send user to another page instead of this one –<% If Request.QueryString(“LoggedIn” =“TRUE” then Response.Redirect “Continue.ASP” Else Response.Redirect “LoginPage.ASP” End IF

12 Global.ASA file The Global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables, and methods that can be accessed by every page in an ASP application. All valid browser scripts (JavaScript, VBScript, JScript, PerlScript, etc.) can be used within Global.asa. This is a good place to initialize Global variables for the Application or for a Session

13 Sub Application_OnStart Application("visitors")=0 End Sub Sub Session_OnStart Application.Lock Application("visitors")=Application("visitors")+1 Application.UnLock End Sub Sub Session_OnEnd Application.Lock Application("visitors")=Application("visitors")-1 Application.UnLock End Sub

14 There are online now!

15 That’s All Folks…  For a really great ASP site, go to  http://www.w3schools.com/asp/


Download ppt "Maintaining State in ASP. Problem - How do I maintain state information about the user  Several Methods –Cookies –Session variables –Hidden fields "

Similar presentations


Ads by Google