User Guide
228 Chapter 12 Using the Application Framework
Using Session Variables
Use Session variables when you need the variables 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 you need the variable for
future visits.)
Caution
You must use the
cflock tag to prevent multiple requests from accessing any Session
variable simultaneously. Failure to do so can result in data corruption.
Enabling Session variables
You enable Session variables in the cfapplication tag in your Application.cfm file.
To enable Session variables:
• Set
sessionManagement="Yes"
• Use the name attribute to specify the application’s name.
• Optionally, use the sessionTimeout attribute to set an application-specific
session timeout value. Use the
CreateTimeSpan function to specify the number
of days, hours, minutes, and seconds for the timeout.
The following code is an example of turning on session management:
<!--- Name the application, and turn on Session management
with a 45-minute timeout --->
<cfapplication name="GetLeadApp"
sessionmanagement="Yes"
sessiontimeout=#CreateTimeSpan(0,0,45,0)#>
What is a session?
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 timeout period) after that client’s last connection. However,
because of the stateless nature of the Web, it is 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. In most cases, however, a Web application has no way of
knowing if a user is finished or just lingering over a page.
You can impose some structure on Session variable duration by specifying a timeout
period. If the user does not access a page of the application within this timeout
period, ColdFusion interprets this as the end of the session and clears any variables
associated with that session.