Setting a Session Variable


Welcome, giorgio.

Here is the scripting that sets the session variable:

<%
' First we will make sure something is in the name field passed from the form
If request("name")="" then
    'First, a bit of simple form validation to make sure we have a value
    response.write "<p><center>Hey! This isn't going to work "
    response.write "unless you enter something! ;)"
    response.write "<form>"
    response.write "<input type='button' value='Return to Form' onclick=history.go(-1)>"
    response.write "</form></center>"
Else
    ' Ok, we have a name...the next line is where the cookie is created.
    Session("fname")= request("name")
    response.write "<font face='arial'>"
    response.write "<p><br>Welcome, " & session("fname") & "."
End If
%>
If you want to clear the value of a session variable, you can do this either of two ways:
<% Session("fname")="" %> OR
<% Session.Abandon %>

The former is good if you still need other information from the session, if your session isn't done and you don't want your global.asa (if you're using one) Session_onEnd code to fire. The latter option destroys all objects created by the session, and frees up resources. If you're using a global.asa, session.abandon will cause the Session_OnEnd code to fire, just as it would if the session timed out normally.

A side note here: By default, sessions time out after 20 minutes after the user's last request for a page. This can be modified by using the Timeout property (if you just want it to apply to one particular application); if you want to change this globally you can do this through the MMC or in the registry. If you anticipate particularly heavy traffic on your site, you might want to lower the default timeout; see Microsoft's Optimizing ASP Performance. Also, for more info on sessions, see Charles Carroll's Session Overview & Myths.