ASP
  Home arrow ASP arrow Page 4 - Maintaining Session State With ASP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

Maintaining Session State With ASP
By: Himanshu Khatri
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 29
    2002-08-30

    Table of Contents:
  • Maintaining Session State With ASP
  • The Session Object
  • The Application Object
  • The Global.asa File
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Maintaining Session State With ASP - The Global.asa File


    (Page 4 of 5 )

    The global.asa file is an optional file where you can specify event scripts and declare session and application objects that can be accessed by every page in your ASP application, as we saw in the example on the previous page.

    Note: The global.asa file must be stored in the root directory of the ASP application and each application can only have one global.asa file.

    Standard Events in Global.asa
    In the global.asa file you can tell the application and session objects what to do when the application/session starts and what to do when the application/session ends. The code for this is placed into event handlers. The global.asa file can contain four types of events:

    1. Application_OnStart: This event occurs when the FIRST user calls the first page from an ASP application. This event also occurs after the web server is restarted or after the global.asa file is edited. When this procedure is complete, the "Session_OnStart" procedure runs.
    2. Session_OnStart: This event occurs EVERY time a user visits your web site and requests the first page.
    3. Session_OnEnd: This event occurs EVERY time a user ends a session. A user ends a session after a page has not been requested by the user for a specific amount of time (this is 20 minutes by default).
    4. Application_OnEnd: This event occurs after the LAST user has ended the session. Typically, this event occurs when a web server is stopped/restarted. This procedure is used to clean up settings after the application stops, such as deleting records or writing log information to text files.
    Example:

    A global.asa file with empty event scripts would look like this:

    <script language=vbscript runat=server>

    SUB Application_OnStart
    END SUB

    SUB Application_OnEnd
    END SUB

    SUB Session_OnStart
    END SUB

    SUB Session_OnEnd
    END SUB

    </script>


    In this example we will create a global.asa file that counts the number of current visitors:
    • The Application_OnStart sets the Application variable visitors to 0 when the server starts
    • The Session_OnStart subroutine adds one to the variable visitors every time a new visitor arrives
    • The Session_OnEnd subroutine subtracts one from visitors each time this subroutine is triggered
    The Global.asa file:

    <script language="vbscript" runat="server">

    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

    </script>


    To display the number of current visitors in an ASP file:

    <html>
    <head>
    </head>
    <body>
    <p>

    There are <%response.write(Application("visitors"))%>
    online now!

    </p>
    </body>
    </html>


    Maintaining Client State With Cookies
    If a visitor comes to your site and types his name into a form then you might want to remember that information. You may only want to remember it for his current visit (session), or for all subsequent visits for personalization ( i.e. "Welcome back Mike"). The term state describes all client browser data for the session. ASP uses client browser side cookies to remember (or persist) this data.

    Cookies are small text files stored on the visitors PC, usually in %root% \ Windows \ Temporary Internet Files \ Cookie:user_ name@Host_Name file. These text files are editable with notepad or Microsoft Word. Two types of information are stored in the cookie files:
    1. Client Data: The variables that make up the cookie for the visitor. These are stored as name/value pairs, such as name=john.
    2. Unique Cookie ID: This allows the ASP Session to identify the client browser on a page to page and visit to visit basis.
    Example:

    <%
    Response.Cookies("myCookie")("myValue1") = 1
    %>


    ... and then to retrieve the value of the cookie:

    <% =Request.Cookies("myCookie")("myValue1") %>

    More ASP Articles
    More By Himanshu Khatri


     

    ASP ARTICLES

    - Central Scoreboard with Flash and ASP
    - Calorie Counter Using WAP and ASP
    - Creating PGP-Encrypted E-Mails Using ASP
    - Be My Guest in ASP
    - Session Replacement in ASP
    - Securing ASP Data Access Credentials Using t...
    - The Not So Ordinary Address Book
    - Adding and Displaying Data Easily via ASP an...
    - Sending Email From a Form in ASP
    - Adding Member Services in ASP
    - Removing Unconfirmed Members
    - Trapping HTTP 500.100 - Internal Server Error
    - So Many Rows, So Little Time! - Case Study
    - XDO: An XML Engine Class for Classic ASP
    - Credit Card Fraud Prevention Using ASP and C...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT