Usually when you code an ASP script, the variables you use only exist while the script runs. But what if you want to code a shopping cart, or something else where data is kept active?
You could start storing all the data in a database, but this takes up space and uses time. It's much easier to work with the session variable space. By default, this space stays active for a given user for 20 minutes.
The session begins as soon as a given computer has a session value stored for them. This creates a cookie for that computer, logging the date and time of entry. From that point forward, every new session value change they do updates the 20 minute timer. If you want to change the timeout value, use:
or whatever minute number you want to use. Remember, session values take up memory. So you want this to be long enough that the user can get through their task, but short enough that you do not have many different sessions open without reason.
To use a session variable, simply put data into it. If a user enters their age and you want to track this for as long as the user is using your pages (to keep them away from mature material, for example) you would use:
Session("age") = 25
to assign it, and then
if Session("age") >= 21 ...
to work with it. Typical uses for the session variable are an age, a log-in user ID and password, shopping items in a shopping cart, and flash vs html user preferences.
If you're done with a given session and want to end it early, perhaps because the user chose a "log off" option on your site, you would use
to completely end that session's run.
|Introduction to ASP Ebook|
Download this ebook to get everything you need to know about learning ASP - from a step by step tutorial to function lists, sample code, common errors and solutions, and much more! 101 pages.