Home arrow ASP arrow Page 4 - Maintaining Session State With ASP
ASP

Maintaining Session State With ASP


Sessions and cookies can be used to persist visitor information. In this article Himanshu shows us how to main session state with ASP using a combination of cookies, sessions and the global.asa file.

Author Info:
By: Himanshu Khatri
Rating: 5 stars5 stars5 stars5 stars5 stars / 82
August 30, 2002
TABLE OF CONTENTS:
  1. · Maintaining Session State With ASP
  2. · The Session Object
  3. · The Application Object
  4. · The Global.asa File
  5. · Conclusion

print this article
SEARCH DEVARTICLES

Maintaining Session State With ASP - The Global.asa File
(Page 4 of 5 )

The global.asa file is an optional file where you can specify event scripts and declare session and application objects that can be accessed by every page in your ASP application, as we saw in the example on the previous page.

Note: The global.asa file must be stored in the root directory of the ASP application and each application can only have one global.asa file.

Standard Events in Global.asa
In the global.asa file you can tell the application and session objects what to do when the application/session starts and what to do when the application/session ends. The code for this is placed into event handlers. The global.asa file can contain four types of events:

  1. Application_OnStart: This event occurs when the FIRST user calls the first page from an ASP application. This event also occurs after the web server is restarted or after the global.asa file is edited. When this procedure is complete, the "Session_OnStart" procedure runs.
  2. Session_OnStart: This event occurs EVERY time a user visits your web site and requests the first page.
  3. Session_OnEnd: This event occurs EVERY time a user ends a session. A user ends a session after a page has not been requested by the user for a specific amount of time (this is 20 minutes by default).
  4. Application_OnEnd: This event occurs after the LAST user has ended the session. Typically, this event occurs when a web server is stopped/restarted. This procedure is used to clean up settings after the application stops, such as deleting records or writing log information to text files.
Example:

A global.asa file with empty event scripts would look like this:

<script language=vbscript runat=server>

SUB Application_OnStart
END SUB

SUB Application_OnEnd
END SUB

SUB Session_OnStart
END SUB

SUB Session_OnEnd
END SUB

</script>


In this example we will create a global.asa file that counts the number of current visitors:
  • The Application_OnStart sets the Application variable visitors to 0 when the server starts
  • The Session_OnStart subroutine adds one to the variable visitors every time a new visitor arrives
  • The Session_OnEnd subroutine subtracts one from visitors each time this subroutine is triggered
The Global.asa file:

<script language="vbscript" runat="server">

Sub Application_OnStart
Application("visitors")=0
End Sub

Sub Session_OnStart
Application.Lock
Application("visitors")=Application("visitors")+1
Application.UnLock
End Sub

Sub Session_OnEnd
Application.Lock
Application("visitors")=Application("visitors")-1
Application.UnLock
End Sub

</script>


To display the number of current visitors in an ASP file:

<html>
<head>
</head>
<body>
<p>

There are <%response.write(Application("visitors"))%>
online now!

</p>
</body>
</html>


Maintaining Client State With Cookies
If a visitor comes to your site and types his name into a form then you might want to remember that information. You may only want to remember it for his current visit (session), or for all subsequent visits for personalization ( i.e. "Welcome back Mike"). The term state describes all client browser data for the session. ASP uses client browser side cookies to remember (or persist) this data.

Cookies are small text files stored on the visitors PC, usually in %root% \ Windows \ Temporary Internet Files \ Cookie:user_ name@Host_Name file. These text files are editable with notepad or Microsoft Word. Two types of information are stored in the cookie files:
  1. Client Data: The variables that make up the cookie for the visitor. These are stored as name/value pairs, such as name=john.
  2. Unique Cookie ID: This allows the ASP Session to identify the client browser on a page to page and visit to visit basis.
Example:

<%
Response.Cookies("myCookie")("myValue1") = 1
%>


... and then to retrieve the value of the cookie:

<% =Request.Cookies("myCookie")("myValue1") %>
blog comments powered by Disqus
ASP ARTICLES

- Central Scoreboard with Flash and ASP
- Calorie Counter Using WAP and ASP
- Creating PGP-Encrypted E-Mails Using ASP
- Be My Guest in ASP
- Session Replacement in ASP
- Securing ASP Data Access Credentials Using t...
- The Not So Ordinary Address Book
- Adding and Displaying Data Easily via ASP an...
- Sending Email From a Form in ASP
- Adding Member Services in ASP
- Removing Unconfirmed Members
- Trapping HTTP 500.100 - Internal Server Error
- So Many Rows, So Little Time! - Case Study
- XDO: An XML Engine Class for Classic ASP
- Credit Card Fraud Prevention Using ASP and C...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials