To use Client variables effectively, you set up the Client variable options and use the variables on your ColdFusion pages.
If you want to enable Client variables, you must do so on every page in an application. Because the Application.cfm file is included in all of the application's pages, you enable client management in the cfapplication
tag, at the beginning of Application.cfm.
<!--- This example illustratescfapplication
---> <!--- Name the application and enable client management---> <cfapplication
NAME="SearchApp" clientmanagement="Yes">
After you enable client state management, you must determine where you want to store Client variables. The system-wide default is to store Client variables in the Registry. But you can choose to store them instead in a SQL database or in cookies.
The ColdFusion Administrator Server Client Variables page controls the default Client variable location. You can override the default location by specifying a clientstorage
attribute in the cfapplication
tag.
You can specify the following values in the clientStorage
attribute:
Registry
(the default)
Cookie
As a general rule, it is most efficient to store Client variables in a database.
When you set clientstorage="Cookie"
the cookie that ColdFusion creates has the application's name. Storing client data in a cookie is scalable to large numbers of clients, but this storage mechanism has some limitations. Chief among them is that if the client turns off cookies in the browser, Client variables do not work.
Consider these additional limitations before implementing cookie storage for Client variables:
cfglobals
to hold global data about the client, such as HitCount
, TimeCreated
, and LastVisit
. This limits you to 17 unique applications per host.
The following example shows how to enable Client variable management using a sample database called mydatasource.
<!--- This example illustratescfapplication
---> <!--- Name the application and enable client management---> <cfapplication
NAME="SearchApp" clientmanagement="Yes" clientstorage="mydatasource">
Note Client storage mechanisms are exclusive; when one storage type is in use, the values set in other storage options are unavailable. |
When you enable Client variables for an application, you can use them to keep track of long-term information that is associated with a particular client.
To create a Client variable and set the value of the parameter, use the cfset
or cfparam
tag; for example:
<cfset Client.FavoriteColor="Red">
After you set a Client variable in this manner, it is available for use within any page in your application that is accessed by the client for whom the variable is set.
The following example shows how to use the cfparam
tag to check for the existence of a client parameter and to set a default value if the parameter does not already exist:
<cfparam name="Client.FavoriteColor" default="Red">
You use the same syntax to access a Client variable as other types of variables. You can use Client variables anywhere other ColdFusion variables are used.
To display the favorite color that has been set for a specific user, use the following code:
<cfoutput> Your favorite color is #Client.FavoriteColor#.
</cfoutput>
The Client scope has six built-in, read-only variables that your application can use:
You can use the Client.HitCount
and time information variables in customizing behavior, depending on how often users visit your site and when they last visited. For example, the following code shows the date of a user's last visit to your site:
<cfoutput> Welcome back to the Web SuperShop. Your last visit was on #DateFormat(Client.LastVisit)#.
</cfoutput>
To obtain a list of the custom client parameters associated with a particular client, use the GetClientVariablesList
function.
<cfoutput>#GetClientVariablesList()#</cfoutput>
The GetClientVariablesList
function returns a comma-separated list of the names of the Client variables for the current application. The standard system-provided Client variables (CFID, CFToken, URLToken, HitCount, TimeCreated, and LastVisit) are not returned in the list.
Unlike normal variables, Client variables and their values are not stored in volatile memory and persist over time. To delete a Client variable, use the DeleteClientVariable
function; for example:
<cfset IsDeleteSuccessful=DeleteClientVariable("MyClientVariable")>
The DeleteClientVariable
function deletes only Client variables for the application specified by cfapplication
, if any. For more information on this function, see the CFML Reference.
Also, using the Client Variables page of the ColdFusion Administrator Server tab, you can edit the Client variable storage to remove Client variables stored in either the Registry or a database after a set number of days. (The default value is 90 days when Client variables are stored in the registry, 10 days when stored in a data source.)
For more information about setting timeout values, see Advanced ColdFusion Administration.
Note You cannot delete the system-provided Client variables (CFID, CFToken, URLToken, HitCount, TimeCreated, and LastVisit). |
If you use the cflocation
tag to redirect to a path that ends in .dbm or .cfm, the Client.URLToken is automatically appended to the URL. You can suppress this behavior by adding the attribute addtoken="No"
to the cflocation
tag.
All Client variable reads and writes are cached to help decrease the overhead of client state management operations. For information on variables and server clustering, see Advanced ColdFusion Administration.
If your Client variable database is stored in the Windows system registry and you need to move it to another machine, you can export the registry key that stores your Client variables and take it to your new server. The system tegistry allows you to export and import Registry entries.
HKEY_LOCAL_MACHINE\SOFTWARE\Allaire\ColdFusion\
CurrentVersion\Clients
After you create a registry file, you can copy it to a new machine and import it by selecting Import Registry File on the Registry Editor Registry menu.
On UNIX systems, the registry entries are kept in
/opt/coldfusion/registry/cf.registry, a text file that you can copy and edit directly.