ASP
  Home arrow ASP arrow Page 2 - Introduction To The GLOBAL.ASA For ASP Pro...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

Introduction To The GLOBAL.ASA For ASP Programmers
By: Rich Smith
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 51
    2003-07-16

    Table of Contents:
  • Introduction To The GLOBAL.ASA For ASP Programmers
  • Inside the GLOBAL.ASA File
  • Application and Session Objects
  • Type libraries for COM

  • 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


    Introduction To The GLOBAL.ASA For ASP Programmers - Inside the GLOBAL.ASA File


    (Page 2 of 4 )

    Lets get a little more detailed about what can be contained inside the GLOBAL.ASA. For starters, this file can contain certain session and application events. These events tell the internal application and session objects what to do when they start and end. Below are the four possible events that may be included in this file:

    • Application_OnStart – This event is called the FIRST time any user hits an ASP page in the same tree as the GLOBAL.ASA. The first hit of any user is considered to be when the application is first invoked. This function is good for loading any default values for application variables, and initializing logs files and such. The application state is reset after the Web Server is restarted, or when the GLOBAL.ASA file is modified. The Session_OnStart will automatically fire when this event finishes.

    • Session_OnStart – This event is called the first time each new user hits the ASP application. So, for each session that is created, this event is called. This event is good for setting up any session level defaults or variables that will be needed for the duration of the user session.

    • Session_OnEnd – This even will fire when a user ends a session. This can be done by a user logging out manually, or via a specified timeout period (by default 20 minutes). Useful for session cleanup code.

    • Application_OnEnd – This final event occurs when the last user ends his or her session. This process normally fires when the web server is being shut down. This is useful for closing files, creating reports, and updating certain usage statistics.

    Here is an example of using the GLOBAL.ASA to create a simple hit counter:


    <SCRIPT language=vbscript runat="server">

    Sub Application_OnStart
      getcounter
    End Sub

    Sub Session_OnStart
      Application
    .Lock
      Application
    ("currentCount")=Application("CurrentCount")+1
      Application
    .UnLock
    End Sub

    Sub Application_OnEnd
      Writecounter
    End Sub

    sub getcounter
      set conn
    =Server.CreateObject("ADODB.Connection")
      
    conn.Provider="Microsoft.Jet.OLEDB.4.0"
      
    conn.Open "c:/webdata/counter.mdb"
      
    set rs=conn.execute("select currentcount from datatable”)
      Application("
    currentCount")=rs(“currentcount”)
      rs.Close
      conn.Close
    end sub

    sub writecounter
      set conn=Server.CreateObject("
    ADODB.Connection")
      conn.Provider="
    Microsoft.Jet.OLEDB.4.0"
      conn.Open "
    c:/webdata/counter.mdb"
      conn.execute("
    update datatable set currentcount “ &  
        
    application(“currentcount”))    
      
    conn.Close
    end sub

    </SCRIPT>
     



    In order to use the counter value on any webpage, you would do the following:

     This is my web page There have been <%response.write(Application("currCount"))%>Visitors to this website



    You will notice in the GLOBAL.ASA example above that I actually use subroutines that are called from the main events. I did this for code readability, and it is a common practice. You will also notice that there are no <% and %> tags inside the GLOBAL.ASA file. In this file, you must place all script within the <script> tag.

    More ASP Articles
    More By Rich Smith


     

    ASP ARTICLES

    - Using MySQL with ASP
    - ADO for the Beginner
    - ADO.NET 101: Data Rendering with a DataGrid ...
    - Introducing SoftArtisans OfficeWriter 3.0 En...
    - Getting Remote Files With ASP
    - The Real Basics of Functions in ASP
    - Enhancing Readability with ASP
    - Mimicking PHP's String Formatting Functions
    - Windows Server Hacks 12, 77, and 98
    - How to Sort a Multi-Dimensional Array
    - Developing an Information Management Tool wi...
    - What are Active Server Pages?
    - Getting Remote Pages with ASP
    - FTP’ing Files with ASP
    - Apply Single-Sign-On to Your Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT