Use session variables when the variables are needed for a single site visit or set of requests. For example, you might use session variables to store a user's selections in a shopping cart application. (Use client variables when the variable is needed for future visits.)
A session refers to all the connections that a single client might make to a server in the course of viewing any pages associated with a given application. Sessions are specific to individual users. As a result, every user has a separate session and has access to a separate set of session variables.
This logical view of a session begins with the first connection by a client and ends (after a specified time-out period) after that client's last connection. However, because of the stateless nature of the Web, it's not always possible to define a precise point at which a session ends. In the real world, a session ends when the user finishes using an application and goes off to do something else. In most cases, however, a Web application has no way of knowing if a user is finished or if he's just lingering over a page.
You can impose some structure on session variable duration by specifying a time-out period. If the user does not access a page of the application within this time-out period, ColdFusion interprets this as the end of the session and clears any variables associated with that session.
The default time-out for session variables is set to 20 minutes. In the Variables page of the ColdFusion Administrator, you can change this time-out value. See Administering ColdFusion Server for more information.
You can also set the time-out period for session variables inside a specific application (thereby overruling the Administrator default setting) by using the SESSIONTIMEOUT attribute of the CFAPPLICATION tag.
Session variables are designed to store session-level data. They are a convenient place to store information that all pages of your application might need during a user session. Using session variables, an application could initialize itself with user-specific data the first time a user hit a page of that application. This information could then remain available while that user continues to use that application. For example, information about a specific user's preferences could be retrieved from a database once, the first time a user hits any page of an application. This information would remain available throughout that user's session, thereby avoiding the overhead of retrieving the preferences again and again.
Session variables work exactly as client variables do, in that they require a client name (client ID) and are always scoped within that client ID. Session variables also work within the scope of an application name if one is supplied, in which case their scope will be the combination of the client ID and the application name.
To enable session variables, set SESSIONMANAGEMENT="Yes"
in the CFAPPLICATION tag in your Application.cfm
file. Note that when you turn on session management in the CFAPPLICATION tag, you must specify the application's name. Following is an example of turning on session management:
<!--- This example illustrates CFAPPLICATION ---> <!--- Name the application, and turn on session management ---> <CFAPPLICATION NAME="GetLeadApp" SESSIONMANAGEMENT="Yes"> <!--- set data source for this application ---> <CFSET dsn = "my_dsn"> <!--- set global error handling for this application ---> <CFERROR TYPE="REQUEST" TEMPLATE="request_err.cfm" MAILTO="webmaster@mysite.com"> <CFERROR TYPE="VALIDATION" TEMPLATE="val_err.cfm" MAILTO="webmaster@mysite.com"> <!--- set some global variables for this application to be triggered on every page ---> <CFSET MainPage = "default.cfm"> <CFSET session.current_location = "Davis, Porter, Alewife"> <CFSET sm_location = "dpa"> <CFSET current_page = "#cgi.path_info#?#cgi.query_string#">