Enabling Session Variables in ColdFusion
by Ron Kurtus (revised 26 September 2018)
The standard variables used in ColdFusion can be only transferred or sent to the next page before it is necessary to restate the variable. In some situations, you may want to define a variable that will apply to all the pages during a single session of the user.
Enabling Session Variables in ColdFusion allows you to keep a variable active during a user session.
An example is when the pages a user sees are personalized to his or her specific needs. In such a case, session variables are defined and used.
Questions you may have include:
- What are session variables?
- How are they applied to a given session?
- What is an example of the use of session variable?
This lesson will answer those questions.
Session variable persistent
A session variable is one of several types of variables that persist across multiple templates:
- Server variables - Accessible by all clients and applications on a single
- Application variables - Tied to a single application and accessible by multiple clients
- Client variables - Tied to a single client over multiple sessions
- Session variables - Exist for one client or browser during a single session
- Cookie variables
Session variables are designed to hold information that you seldom write but are read often.
Define in Application.cfm template
A common method of using session variables is to define them in the Application.cfm template, which is a special ColdFusion page that is processed before the other pages in a session. It usually should be in the session root directory.
To enable the use of session variables, as well as client and application management, you should use the CFAPPLICATION tag in the Application template. A typical tag would be:
SESSIONTIMEOUT="#CreateTimeSpan(0, 0, 20, 0)#">
- CFAPPLICATION NAME is required to avoid problems if you have session variables tied to separate applications.
- SESSIONMANAGEMENT is required to enable the session variables.
- SESSIONTIMEOUT is optional and limits the time the variables will stay in memory (20 minutes in the example) Note that the default is 20 minutes, so you really may not need this unless you want to change that number.
After the CFAPPLICATION tag, you can set your session variables, using the CFSET tag. You must always refer to session variables with the prefix session. Thus, you could define a session variable, such as:
Should lock variables
You should lock the session variables to avoid problems when several people are using the system at the same time. An example of this is:
<CFLOCK TIMEOUT="30" NAME="#session.sessionID#" TYPE="Exclusive">
Defined on applicable pages
A problem in using the Application.cfm template is that it is often difficult to change your session variables, once they have been set. An alternative is to use the CFAPPLICATION tag in each applicable page:
You then can define the session variable in the first page, accessed:
This is a compromise between the standard method and defining the variable on each page.
Example of use
Suppose a user logged in to the site. His name could be sent through a form and entered in Application.cfm. Then the session will constantly refer to him by name.
<CFAPPLICATION NAME="Namex" SESSIONMANAGEMENT="Yes">
Session variables is a handy way to define a persistent variable.
Observe and learn
Resources and references
Configuring and using session variables - Adobe Help
Questions and comments
Do you have any questions, comments, or opinions on this subject? If so, send an email with your feedback. I will try to get back to you as soon as possible.
Share this page
Click on a button to bookmark or share this page through Twitter, Facebook, email, or other services:
Students and researchers
The Web address of this page is:
Please include it as a link on your website or as a reference in your report, document, or thesis.
Where are you now?
Using Session Variables in Coldfusion