User Guide

Table Of Contents
356 Chapter 15: Using Persistent Data and Locking
Creating and deleting session variables
Use 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 Macromedia TechNote 22712 at
www.macromedia.com/v1/Handlers/index.cfm?ID=22712&Method=Full.
Accessing and changing session variables
You use the same syntax to access a session variable as for other types of variables. However, you
must lock any code that accesses or changes session variables.
For example, to display the number of items in a users shopping cart, use favorite color that has
been set for a specific user, for example, use the following code:
<cflock timeout=20 scope="Session" type="Exclusive">
<cfoutput>
Your shopping cart has #Session.ShoppingCartItems# items.
</cfoutput>
</cflock>
To change 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 session
The following rules apply to ending a session and deleting Session scope variables:
If you use ColdFusion session management, ColdFusion automatically ends sessions and
deletes all Session scope variables if the client is inactive for the session time-out period. The
session does not end when the user closes the browser.
If you use J2EE session management, ColdFusion MX ends the session and deletes all Session
scope variables when the user closes the browser, or if the client is inactive for the session time-
out period. If the session times out, however, the browser continues to send the same session
ID, and ColdFusion will reuse this ID for sessions with this browser instance, as long as the
browser remains active.
Logging a user out does not end the session or delete Session scope variables.
In many cases, you can effectively end a session by clearing the Session scope, as shown in the
following line. The following list, however, includes important limitations and alternatives:
<cfset StructClear(Session)>