

|
|
Variables in CFML; Session and Client ScopesLike the Server and Application scopes the Session and Client scopes are both persistant and can exist across multiple requests. However each of these scopes is linked to a specific client allowing you to store information specific to each user of your application. Like the Application scope these scopes are only enabled by those requests using the <CFAPPLICATION> tag.
Both of these scopes require ColdFusion to uniquely identify end users in order to properly link them to their information. ColdFusion does this, by default, using the combination two generated values CFTOKEN and CFID. By default these values are sent to the client as cookies and will persist indefinitely. The values can also be appended to the URL of each ColdFusion request in those cases where cookies aren't desirable. There are several important points to understand about CFTOKEN/CFID:
The Session and Client scopes can be enabled or disabled using the SESSIONMANAGEMENT and CLIENTMANAGEMENT attributes of the <CFAPPLICATION> tag. As there is some overhead involved with using them it's recommended that they only be enabled if you are planning to use them. With CFMX another option (available in the CFAdministrator) for uniquely linking the end user with the server-side Session (but NOT Client) scopes is to use the J2EE Session Management. This will create JSESSIONID variable at the start of each new session. This identifier can not persist like the CFTOKEN/CFID identifiers but does allow your CFMX application to seamlessly share session data with JSP pages or Java Servlets running on the same server. Another potential benefit of using J2EE session management is that J2EE sessions can be replicated automatically across mutliple servers in a cluster. This eliminates lost sessions due to sever outages and routine maintenance. Sean Corfield, Macromedia's Director of Architecture in IT has a complete discussion on this topic available in his BLOG. Session ScopeThe Session scope is used to maintain data for currently active users. Like the Server and Application scopes the session scope is maintained in RAM and has the same limitations. Data stored in the Session scope can not exist across servers in a cluster and will not persist through a server/service restart.
Unlike some other application server solutions ColdFusion does not allow you to perform any actions upon the session timeout. In other words the session timeout does not throw a trappable event. You can mimic this by manually controlling the session timeout or by keeping (and manually managing) session-type information within the application scope or a database. Although these interim solutions are workable it is hoped that future versions of ColdFusion will allow for true end-of-session management. There are several predefined variables contained in the session scope. These variables will exist in the default configuration:
These variables will exist if J2EE session management is enabled:
Because the Session scope is maintained in RAM it may hold complex data types (structures, queries, etc). In ColdFusion MX you can also place instances of a CFC into the Session scope (a very powerful option). The Session scope is accessible as a structure in ColdFusion 4.5 and above. RecommendationsBecause the Session scope is persistent but not permanent (data won't survive a server reboot) it's recommended that it only be used to cache information and maintain application state. Information that must be collected from a database (especially if the queries involved are slow) or must be calculated are prime candidates for caching. You fetch or calculate the information only once and then store it for access in the session scope. There are many scenarios where storing application state in the session scope can enhance the functionality and usability of an application. Storing user input during a multi-step process or user selections for later use (a shopping cart for example) are common examples of this. Client ScopeThe Client scope is designed to store more permanent information than the Session scope. It is not stored in RAM but rather uses one of several developer specified data storage interfaces. Although the possible storage mechanisms are all available manually the Client scope nicely abstracts them and provides some system generated values. A default storage mechanism can be defined using the ColdFusion Administrator and developer's may also define them at run time using the CLIENTSTORAGE attribute of the <CFAPPLICATION> tag. The available storage options are:
The Client scope provides several predefined, read-only variables for your use:
The ColdFusion function GetClientVariablesList() can be used to return a list of all developer-defined variable names in the Client scope. The predefined variables described above will not be returned. The Client scope is accessible as a structure in CFMX and above. RecommendationsIt was already noted but it's worth repeating that the Registry is simply not a good place to store your Client information. In our opinion a Database is the best possible solution although this does require access to the ColdFusion Administrator (or a web host willing to configure a datasource for you). Cookies are a good option if the application doesn't require the client information to function (for example if the client information will only be used for system metrics or tracking) or if the application users can be forced to use cookies. Whatever the storage mechanism the Client scope is a great abstraction for commonly used data. Although you could set the same information manually in a database or in cookies using the Client scope can be clearer and require much less coding and initial design. |