ColdFusion 9.0 Resources |
Configuring and using session variablesContents [Hide]Use session variables when you need the variables for a single site visit or set of requests. For example, use session variables to store a user’s selections in a shopping cart application. (Use client variables if you need a variable in multiple visits.) Important: Place code that uses session variables
inside cflock tags in circumstances
that could result in race conditions from multiple accesses to the
same variable. For information on using cflock tags see Locking code with cflock.
What is a session?A session refers to all the connections that a single client makes to a server in the course of viewing any pages associated with a given application. Sessions are specific to both the individual user and the application. As a result, every user of an application has a separate session and has access to a separate set of session variables. This logical view of a session begins with the first connection to an application by a client and ends after that client's last connection. However, because of the stateless nature of the web, it is not always possible to define a precise point at which a session ends. A session should end when the user finishes using an application. In most cases, however, a web application has no way of knowing if a user has finished or is just lingering over a page. Therefore, sessions always terminate after a time-out period of inactivity. If the user does not access a page of the application within this time-out period, ColdFusion interprets this as the end of the session and clears any variables associated with that session. The default time-out for session variables is 20 mins. You can change the default time-out on the Memory Variables page in the Server Settings area in the ColdFusion Administrator. You can also set the time-out period for session variables inside a specific application (thereby overruling the Administrator default setting) by setting the Application.cfc This.sessionTimeout variable or by using the cfapplication tag sessionTimeout attribute. However, you cannot set a time-out value for that is greater than the maximum session time-out value set on the Administrator Memory Variables page. For detailed information on ending sessions and deleting session variables, see Ending a session. ColdFusion and J2EE session managementThe ColdFusion server can use either of the following types of session management:
ColdFusion session management uses the same client identification method as ColdFusion client management. J2EE session management provides the following advantages over ColdFusion session management:
Therefore, consider using J2EE session management in any of the following cases:
Configuring and enabling session variablesTo use session variables, enable them in two places:
ColdFusion Administrator, Application.cfc, and the cfapplication tag also provide facilities for configuring session variable behavior, including the variable time-out. Selecting and enabling session variables in ColdFusion AdministratorTo use session variables, they must be enabled on the ColdFusion Administrator Memory Variables page. (They are enabled by default.) You can also use the Administrator Memory Variables page to do the following:
Enabling session variables in your applicationEnable session variables in the initialization code of your Application.cfc file or in the cfapplication tag in your Application.cfm file. Do the following in the Application.cfc initialization code, below the cfcomponent tag, to enable session variables:
Do the following in the Application.cfm file to enable session variables:
The following sample code enables session management for the GetLeadApp application and sets the session variables to time out after a 45-minute period of inactivity: <cfapplication name="GetLeadApp" sessionmanagement="Yes" sessiontimeout=#CreateTimeSpan(0,0,45,0)#> Storing session data in session variablesSession variables are designed to store session-level data. They are a convenient place to store information that all pages of your application might need during a user session, such as shopping cart contents or score counters. Using session variables, an application can initialize itself with user-specific data the first time a user accesses one of the pages of the application. This information can remain available while that user continues to use that application. For example, you can retrieve information about a specific user’s preferences from a database once, the first time a user accesses any page of an application. This information remains available throughout that user’s session, thereby avoiding the overhead of retrieving the preferences repeatedly. Standard session variablesIf you use ColdFusion session variables, the Session scope has four built-in, read-only variables that your application can use. If you use J2EE session management, the Session scope has two built-in variables. Generally, you use these variables in your ColdFusion pages only if your application supports browsers that do not allow cookies. For more information on supporting browsers that do not allow cookies, see Using client and session variables without cookies. The following table describes the built-in session variables.
Note: ColdFusion lets you delete or change the values
of the built-in session variables. As a general rule, avoid doing
so.
If you enable client variables and ColdFusion session management, ColdFusion uses the same values for the Client and Session scope CFID, CFToken, and URLtoken variables. ColdFusion gets the values for these variables from the same source, the client’s CFID and CFTOKEN cookies. If you use J2EE session management, the Session scope does not include the Session.CFID or Session.CFToken variables, but does include the Session.URLToken and Session.SessionID variables. In this case, the Session.SessionID is the J2EE session ID and Session.URLToken consists of the string jsessionid= followed by the J2EE session ID. Getting a list of session variablesUse the StructKeyList function to get a list of session variables, as follows: <cflock timeout=20 scope="Session" type="Readonly"> <cfoutput> #StructKeyList(Session)# </cfoutput> </cflock> Important: Always put code that accesses session variables
inside cflock tags.
Creating and deleting session variablesUse a standard assignment statement to create a new session variable, as follows: <cflock timeout=20 scope="Session" type="Exclusive"> <cfset Session.ShoppingCartItems = 0> </cflock> Use the structdelete tag to delete a session variable; for example: <cflock timeout=20 scope="Session" type="Exclusive"> <cfset StructDelete(Session, "ShoppingCartItems")> </cflock> Note: If you set session variables on a CFML template
that uses the cflocation tag, ColdFusion might
not set the variables. For more information, see TechNote at www.adobe.com/go/tn_18171.
Accessing and changing session variablesYou use the same syntax to access a session variable as for other types of variables. However, lock any code that accesses or changes session variables. For example, to display the number of items in a user’s shopping cart, use the following code: <cflock timeout=20 scope="Session" type="Exclusive"> <cfoutput> Your shopping cart has #Session.ShoppingCartItems# items. </cfoutput> </cflock> To increase the number of items in the shopping cart, use the following code: <cflock timeout=20 scope="Session" type="Exclusive"> <cfset Session.ShoppingCartItems = Session.ShoppingCartItems + 1> </cflock> Ending a sessionThe following rules apply to ending a session and deleting Session scope variables:
|