Enabling Session Variables in ColdFusion
About 3 min read
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.
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_x”
SESSIONMANAGEMENT=”Yes”
SESSIONTIMEOUT=”#CreateTimeSpan(0, 0, 20, 0)#”>
where:
- 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.
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=”Namex”
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=”Namex” SESSIONMANAGEMENT=”Yes”>
<CFSET session.namex=”#form.othername#”>
Start.cfm
<CFOUTPUT>
<H1>Hello #session.namex#</H1>
</CFOUTPUT>
Summary
Session variables is a handy way to define a persistent variable.
Observe and learn
Resources and references
Websites
Configuring and using session variables – Adobe Help
