List of Topics

SfC Home > Web Design > SQL > ColdFusion >

 

Using Session Variables in Coldfusion

by Ron Kurtus (revised 12 March 2013)

Enabling Session Variables in ColdFusion allows you to keep a variable active during a user session.

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 include:

This lesson will answer those questions.



Session variable persistent

A session variable is one of several types of variables that persist across multiple templates:

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:

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

Ron Kurtus' Credentials

Websites

Configuring and using session variables - Adobe Help

ColdFusion Resources

Books

Top-rated books on ColdFusion


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

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:
www.school-for-champions.com/coldfusion/
session_variables.htm

Please include it as a link on your website or as a reference in your report, document, or thesis.

Copyright © Restrictions


Where are you now?

School for Champions

ColdFusion topics

Using Session Variables in Coldfusion




ColdFusion topics

Basics

Development methods

Specific applications

Also see



Let's make the world a better place

Be the best that you can be.

Use your knowledge and skills to help others succeed.

Don't be wasteful; protect our environment.

You CAN influence the world.





Live Your Life as a Champion:

Take care of your health

Seek knowledge and gain skills

Do excellent work

Be valuable to others

Have utmost character

Be a Champion!



The School for Champions helps you become the type of person who can be called a Champion.