Wife/Girlfriend/Sister?
Pepper Spray &
Stun Gun Specials!
KEEP THEM SAFE.
 CoverYourASP --> Using Session variables to maintain state --> 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
18 active users
15 visitors today
48 pages today
how is this done?
Tools I use

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

ASP.NET Blog
RSS submissions
E-commerce

Now open source with SourceForge!

Tracking new sessions

The server is nice enough to tell us when a new session is created by calling a function in your global.asa file, Session_OnStart( ).

If you don't have a global.asa, just create one - there's nothing magical about the file. A simple example is shown below:

<script language=JavaScript runat=server>

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' )++;

   Application.Unlock ( );
}

</script>

In this example, as on my site, I use the Session_OnStart( ) function to increment a count of how many users are currently on my site, or more accurately how many active sessions there are (you'll see in a minute that there are always more active sessions than users on your site...).

There are no restrictions on what you can do in this function - you could initialize some Session variables in here, connect to your database, send emails - whatever.

Part 4: Ending a session...

Featured sponsor
My favorite resources

Tiki Statues - Tiki Masks - Tiki Totems



Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!

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?