Many people develop Active Server Pages using Session Variables. These variables are strikingly similar to global variables in any programming language, and they present the same advantages and pitfalls that global variables present. There is a time and a place for everything (even GOTO's), and Session variables do have their time and place. Overuse of Session variables, however, leads to unmaintainable code and increased server expenses. Below are my personal pros and cons for the use of Session variables.
- If you have a variable that needs to be passed around to a lot of web pages, it may simplify things to use a Session variable, rather than passing the variable around through the QueryString.
- Session variables allow for customization of a web site. Each visitor to your site can have a customized experience.
While this is true, it is also true that with the advent of LDAP and items
such as MS Site Server and the like, it is no longer necessary to put all
of your customization that is dependent upon user preference into session variables.
- Session variables take you one step closer to VB programming in the sense that you can grab one without initializing the variable, use it whenever you want to, and not have to worry about releasing it when you've finished using it.
- Session variables and cookies are synonymous. So if a user has set his browser not to accept any cookies, your Session variables won't work for that particular web surfer!
- An instance of each session variable is created when a user
visits the page, and these variables persist for 20 minutes AFTER the user
leaves the page!
(Actually, these variables persist until they "timeout". This timeout
length is set by the web server administrator. I have seen sites that the
variables will collapse in as little as 3 minutes, and others that persist
for 10, and still others that persist for the default 20 minutes.
So, if you put any large objects in the Session (such as
ADO recordsets, connections, etc.), you are asking for serious trouble!
As the number of visitors increase, your server will experience dramatic
performance woes by placing large objects in the Session!
- Since Session variables can be created on the fly, used whenever, and do not require the developer to dispose of them explicitly, the overuse of Session variables can lead to very unreadable and unmaintainable code.
- Session variables take you one step closer to VB programming in the sense that you can grab one without initializing the variable, use it whenever you want to, and not have to worry about releasing it when you've finished using it. And WHO wants to go there? Not me.
There are advantages and disadvantages to using Session variables. Personally, I would recommend to stay away from them, but there are places where they do greatly simplify web development. The choice to use Session variables is obviously yours, but before you use them or choose not to use them, you should be well aware of both the pros and cons. Happy Programming!