ASP/.NET Web Hosting
* 3 Months FREE *
CLICK HERE!
 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
161 active users
3107 visitors today
3022 pages today
(only part of today)
ASP.NET Showcase
ASP.NET Graph and Chart Component with FREE developer version

ASP.NET Shopping Cart and E-commerce Control with FREE trial download

ASP.NET Web Hosting with free month and setup

$15 Domain Name Registration from .DNR.
As seen on these sites
Special thanks go to the sites that support me by publishing my articles.
See more

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

ASP.NET Blog
ASPRSS
ShawThing

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

Selling products online - read how James does it now


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?


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!