![]() ![]() ![]() |
Session and application variables are persistent variable "scopes." You access these variables by prefacing the variable name with the scope name, for example: "Session.MyVariable" or "Application.MyVariable." And because they are persistent, you can pass values between pages with a minimum of effort.
Session and application variables are similar in operation to client variables. Like client variables, they are enabled with the CFAPPLICATION tag. See the CFML Language Reference for more information on the CFAPPLICATION tag.
Unlike client variables, however, which are stored in either the system registry, a datasource, or a cookie, application and session variables are always stored in the ColdFusion server's memory. This method offers obvious performance advantages. In addition, you can set time-out values for these variables either with CFAPPLICATION, or by specifying time-outs in the ColdFusion Administrator. You can also simply disable application and session variables entirely.
For information on setting time-outs for variables, see the Administering ColdFusion Server book.
Use session variables when the variables are needed for a single site visit or set of requests. For example, you might use session variables to store a user's selections in a shopping cart application. (You would use client variables if the variable is needed for future visits.)
Session variables are designed to store session-level data. They are a convenient place to store information that all pages of your application might need as long as a single user is running that application. Using session variables, an application could initialize itself with user-specific data the first time a user hit a page of that application. This information could then remain available while that user continues to use that application. For example, information about a specific user's preferences could be retrieved from a database once, the first time the user hits any page of an application. This information would remain available throughout that user's session, thereby avoiding the overhead of retrieving the preferences again and again.
Session variables work exactly as client variables do, in that they require a client name (client ID) and are always scoped within that client ID. Session variables also work within the scope of an application name if one is supplied, in which case their scope will be the combination of the client ID and the application name.
To enable session variables, set SESSIONMANAGEMENT="Yes"
in the CFAPPLICATION tag in your Application.cfm
file.
You create session variables using the CFSET tag. See Using CFSET to create variables in this chapter.
Session variables have a specific lifetime, and it is this lifetime that defines a "session." For example, when you access a session variable within its specified lifetime, the variable returns a value because your request occurs during a single, time-limited session. On the other hand, if you do not access a session variable within its specified lifetime, the variable will time-out and will no longer be available to you.
The default time-out for session variables is set to 20 minutes. In the Variables page of the ColdFusion Administrator, you can change this time-out value. See the Administering ColdFusion Server book for more information.
You can also set the time-out period for session variables inside a specific application (thereby overruling the Administrator default setting) by using the SESSIONTIMEOUT attribute of the CFAPPLICATION tag.
An application refers to all the related web pages that make up a particular piece of functionality. In ColdFusion, you define an application by giving it a name using the CFAPPLICATION tag. By using the same application name in a CFAPPLICATION tag, a set of templates can define themselves as being part of the same logical application.
The value you set for the NAME attribute in CFAPPLICATION is limited to 64 characters.
You create application variables using the CFSET tag. See Using CFSET to create variables in this chapter.
Application variables are designed to store application-level data. They are a convenient place to store information that all pages of your application might need no matter who (what client) is running that application. Using application variables, an application could initialize itself, say, when the first user hits any page of that application. This information could then remain available indefinitely to all subsequent hits of any pages of that application, thereby avoiding the overhead of repeated initialization.
Because the data stored in application variables are available to all pages of an application and remains available indefinitely, or until the ColdFusion server shuts down, application variables are very convenient. However, because all clients running an application see the same set of application variables, they are not useful for client-specific information. If you wish to do the same sorts of things, but on a client-specific basis, session variables are for you.
Application variables work very much as client variables do, except that they require an application name be associated with them. (Thus, they are always scoped within that application name.)
Unlike client variables, however, they do not require that a client name (client ID) be associated with them. Thus, they are available to any clients that specify the same application name.
Here's a table that maps out these relationships:
Client, Session, and Application Variables | |||||
---|---|---|---|---|---|
Variable Type |
Application Names |
ClientIDs |
Client Mgmt |
Session Mgmt |
Time-out |
Client |
Optional |
Required |
Required |
n/a |
Optional |
Session |
Optional |
Required |
Required |
Required |
Optional |
Application |
Required |
n/a |
n/a |
n/a |
Optional |
Application variables have a specific lifetime, and it is this lifetime that defines an "application." For example, when you access an application variable inside a specific application, the variable returns a value because your request occurs on a page declared in the CFAPPLICATION tag to be part of a single application.
The default time-out period for application variables is two days. In the Variables page of the ColdFusion Administrator, you can define time-out values for application and session variables. See the Administering ColdFusion Server book for more information.
You can also set the time-out period for application variables inside a specific application (thereby overruling the Administrator default setting) by using the APPLICATIONTIMEOUT attribute of the CFAPPLICATION tag.
ColdFusion does not attempt to automatically evaluate Application and Session variables. You must use variable prefixes with these variables, as in Session.variablename or Application.variablename.
For more information about application and session variables, see the Using the Application Framework chapter in this book.
![]() ![]() ![]() |
AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.