SfC Home > ColdFusion >
Explanation of how to use session variables with the ColdFusion markup language. Also refer to Allaire, database, client, server, ODBC, Oracle, e-commerce, education, data management, Ron Kurtus, School for Champions. Copyright © Restrictions
Using Session Variables
by Ron Kurtus (revised 11 January 2001)
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. 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 about session variables are:
- Exactly, 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. There is a mini-quiz near the end of the lesson.
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.
Defining session variables
Session variables are normally defined in the Application template, but can be also defined on all applicable pages.
Application template
The standard 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.
CFAPPLICATION tag
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:
<CFAPPLICATION NAME="Name"
SESSIONMANAGEMENT="Yes"
SESSIONTIMEOUT="#CreateTimeSpan(0, 0, 20, 0)#">
where:
- 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.
Setting variables
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:
<CFSET session.name="#form.othername#">
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">
<CFSET session.name="#form.othername#">
</CFLOCK>
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:
<CFAPPLICATION NAME="Name"
SESSIONMANAGEMENT="Yes">
You then can define the session variable in the first page, accessed:
<CFOUTPUT QUERY="return">
<CFSET session.ID="#ID#">
</CFOUTPUT>
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.
Application.cfm
<CFAPPLICATION NAME="Name"
SESSIONMANAGEMENT="Yes">
<CFSET session.name="#form.othername#">
Start.cfm
<CFOUTPUT>
<H1>Hello #session.name#</H1>
</CFOUTPUT>
Summary
Session variables is a handy way to define a persistent variable.
Observe and learn
Resources
The following resources provide information on this subject.
Websites
Books
Mini-quiz to check your understanding
1. Why would you use session variables?
2. What is the Application template?
3. Why must you set your session management to "yes"?
If you got all three correct, you are on your way to becoming a Champion in ColdFusion Development. If you had problems, you had better look over the material again.
What do you think?
Do you have any questions, comments, or opinions on this subject? If so, send an email with your feedback. We will try to get back to you as soon as possible.
Share link
Feel free to establish a link from your website to pages in this site.
Or use our form to send this link to yourself or a friend.
Students and researchers
The Web address of this page is
www.school-for-champions.com/coldfusion/sessionvariable.htm. Please
include it as a reference in your report, document, or thesis.
Where can you go from here?
Session Variables
