ASP/.NET Web Hosting
* 3 Months FREE *
CLICK HERE!
 CoverYourASP --> Using Session variables to maintain state --> Part 4

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
151 active users
3123 visitors today
4687 pages today
how is this done?
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!

Ending a session

I've shown how a new session starts, but how does it end? There are two ways:

1. The user doesn't request or refresh a page within a specific time period.
2. Session.Abandon( ) is called from your ASP page.

Sessions timing out

By default, if a user doesn't make any requests from the server for 20 minutes that session is ended. Similar to before, the Session_OnEnd( ) function in global.asa is called at that point. Note that you can only use the Application, Session and Server objects in that function.
Setting timeout for the entire application

You can change the timeout value for all your pages by configuring IIS. On earlier versions of IIS on Windows NT you changed this setting in the Internet Service Manager, but that was renamed to the "Internet Information Services snap-in" in Windows 2000.

To find the IIS snap-in go to Control Panel / Administrative Tools / Internet Services Manager and then view the properties for the Default Web Site. On that dialog go to the Home Directory tab, and choose the Configuration button (nearly there!). Choose the App Options tab, and <phew> there's the setting!

Set it to as small a number as possible to increase the efficiency of your server. Set it larger than the time you expect users to read your largest page, or they could lose their session!

Setting timeout for a single session
You can also override the timeout for a single session by adding the following line into your code (although I can't think why you'd want to do this):

// this session will timeout after 5 minutes inactivity
Session.Timeout = 5;

Abandoning a session

You can end a session (almost) immediately by calling Session.Abandon( ). The rest of the page is still executed, and the Session object is still available for that page, but the session is ended when the page finishes executing. If you want the page to stop processing immediately too, call Response.End( ).

Remember that even though the session has ended, if the user requests a new page from your site a new session will automatically start.

Featured sponsor
My favorite resources

Selling products online - read how James does it now



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!


Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!