The School for Champions is an educational website that shows you how to achieve your dreams.



Other ColdFusion topics:

Basics

Overview of ColdFusion

ColdFusion Features

Development methods

Setting up ColdFusion

ColdFusion Development Steps

Specific applications

Using Session Variables

Combining the Form and Action Pages

Using Drop-Down Lists

Converting ColdFusion to PDF

Dealing with Dates and Times

Sending a Flash Animation with ColdFusion Email

Changing Data to Telephone Format

Populating a New Database Table from an Old Table

Populating a Matrix Table

Sorting a Table Listing in ColdFusion

Also see:

Weekly Feedback Blog

ColdFusion Survey Results

Flash Development

Website Development

eLearning


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:

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:

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:

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.

Answers to Readers' Questions


Observe and learn


Resources

The following resources provide information on this subject.

Websites

ColdFusion Resources

Books

Top-rated books on ColdFusion


Mini-quiz to check your understanding

1. Why would you use session variables?

If a variable is persistent through a session

If variables change throughout a session

If a session needs variables

2. What is the Application template?

An example of a ColdFusion application

A page that opens first, where you can put special code

Another name for session variables

3. Why must you set your session management to "yes"?

To initiate the use of session variables

To satisfy upper management requirements

You must set it to "no"

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?

School for Champions

ColdFusion topics

Session Variables


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