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
30 active users
1873 visitors today
2419 pages today
(only part of today)
Tools I use

CoverYourASP
Copyright © 1999-2010 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



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!


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...