CoverYourASP --> Application variables --> Part 3

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
129 active users
1941 visitors today
1643 pages today
how is this done?
Tools I use

CoverYourASP
Copyright © 1999-2016 James Shaw.
All rights reserved.

ASP.NET Blog
RSS submissions
E-commerce

Now open source with SourceForge!

Counting visits and page views

Another example where I use Application variables is to implement the fun count of active users seen in the top right corner of every page.

This involved a file not discussed yet on this site - global.asa. This file contains four functions that are called by the server at specific times, as documented in the file shown below.

global.asa

<script language=JavaScript runat=server>

// ============================================
// this function is called when the first user visits the page after the server
// has been rebooted. also called when this file is changed.
// ============================================
function Application_OnStart ( )
{
   // you dont need to lock the Application object
   // in this function - but remember to do it elsewhere...
   //   Application.Lock ( );

   // number of users currently on the site
   Application ( 'ActiveUsers' ) = 0;
   // number of users on the site today
   Application ( 'UsersToday' ) = 0;
   // number of pages viewed today
   Application ( 'PagesToday' ) = 0;
   // has the count been restarted today?
   Application ( 'NewToday' ) = 1;
   // number of headers viewed ever
   Application ( 'HeadersViewed' ) = 0;
   // number of footers viewed ever
   Application ( 'FootersViewed' ) = 0;
   // initialize new stuff in utils/Init.asp
   Application ( 'BrandNewDay' ) = 1;
   // a list of IP addresses that have clicked an ad
   Application ( 'ClickFromIP' ) = '';
   // counters for rotating banners
   Application ( 'CurrentTopBanner' ) = 0;
   Application ( 'CurrentSideBanner' ) = 0;
   Application ( 'CurrentAffiliate' ) = 0;

   // remember todays date
   var d = new Date;
   Application ( 'Today' ) = d.getDate ( );

   // Application.Unlock ( );
}

// ============================================
// this function is called when the server shuts down
// ============================================
function Application_OnEnd ( )
{
}

// ============================================
// this function gets called whenever a new user visits the site. note that
// session variables require the client browser to accept cookies
// ============================================
function Session_OnStart ( )
{
   // you must lock the global Application object
   // when writing to it - ok to read without lock
   Application.Lock ( );

   // one more active user
   Application ( 'ActiveUsers' )++;

   // is it a new day?
   var d = new Date;
   var nDate = d.getDate ( );

   if ( Application ( 'Today' ) == nDate )
   {
      // same day, so increment users today
      Application ( 'UsersToday' )++;
   }
   else
   {
      // new day, so restart count
      Application ( 'Today' ) = nDate;
      Application ( 'UsersToday' ) = 1;
      Application ( 'PagesToday' ) = 1;
      Application ( 'BrandNewDay' ) = 1;

      // and say I havent restarted today
      Application ( 'NewToday' ) = 0;
   }

   Application.Unlock ( );
}

// ============================================
// this function gets called after a user session times out - by default
// after 20 minutes. or you can call Session.Abandon ( )
// ============================================
function Session_OnEnd ( )
{
   // you must lock the global Application object
   // when writing to it - ok to read without lock
   Application.Lock ( );

   // one less active user
   Application ( 'ActiveUsers' )--;

   Application.Unlock ( );
}
</script>

Part 4: Displaying visits and page views...

Featured sponsor
My favorite resources

CoverYourASP Mugs, T-shirts, caps - even Boxer shorts...
I don't make a penny from these, but they're a lot of fun! Don't you need a new mouse mat?


Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!


I share my content

Supporting ASPRSS

Do you need a quick and easy way to link to my articles? All the information you need is published with ASPRSS...


New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!