You are ready to start working with application variables once they have been enabled with the CFAPPLlCATIONtag. In this section you will learn how to create, refer to, retrieve, and delete application variables.
Creating Application Variables
Creating application variables is basically the same as creating session variables, except for the name prefix. Application variables must also be locked. Whenever you read or write an application variable, you need to use the CFLOCKtag around it. CFLOCKis discussed in more detail at the end of this chapter.
Just as with session and client variables, you must use the CFSET or CFPARAM tags to create an application variable. Remember to always scope the variable name with the prefix application.
Referring to Application Variables
Referring to application variables is done just as with session variables. You must use the CFLOCKtag around any block of code containing a reference to an application variable. The CFLOCKshould be a ReadOn1y lock. For more information on the CFLOCKtag, please refer to the end of this chapter.
You must also remember to include the application prefix when referring to application variables. Cold Fusion does not automatically evaluate application variables when no prefix is specified. Here are two examples of referring to the application variable .
Using Built-In Application Variables
There is currently only one built-in application variable that stores the NAME attribute of the application set in the CFAPPLICATIONtag. This variable, Application .ApplicationName,is used to uniquely identify each application. For this reason, you should make sure that each of your applications has a unique name.
Retrieving a List of Application Variables
Retrieving a list of all application variables is done exactly as with session variables. Because application variables are also stored in a structure, you can access them by looping through the structure name application
If you use the following code, you can retrieve an entire list of all application variables, including the built-in application variable Application. ApplicationName.
Deleting the Application Structure
Application variables, like session variables, can be retrieved or deleted by using structures. If you know the specific application variable that you want to delete, you would use the StructDe 1ete function. If you want to delete all application variables, you can use the strucral ear function. Both of these examples are demonstrated in the following code. Remember to surround your code with (FLOCK tags).
There are different reasons why you might want to delete application variables. Most notably, you may want to reset application variables to their default values. If you delete application variables, they will be recreated by the Applicaltion. cfm file the next time it is accessed, effectively resetting the variables to their default value.
Using Server Variables
Server variables aren’t tied to any application; they are tied to a single ColdFusion server and.can be accessed by all clients and applications on that server. They are stored in the server’s RAM until the server has been stopped. You use server variables when you want to create a variable that can be accessed by any client connecting to your server. These variables are persistent and ere lost only if the server is shut down. Therefore, you do not want to store too much data in server variables.
It is important to note that although server variables are stored in RAM (like session and application variables) they cannot be accessed as a structure. So retrieving a list of server variables and deleting server variables cannot be done at this time.
Creating Server Variables
Server variables must be locked when created, just like session and application variables. Whenever you read or write a server variable, you need to use the CFLOCK tag around it. CFLOCK is discussed in more detail at the end of this chapter. As with all other variable scopes, you must use the CFSET or CFPARAM tags to create a server variable. You must also preface your variable name with the prefix. The following code sets a string equal to the server variable server. season.
Referring to Server Variables
Referring to server variables is done just as with session and application vanables. You must use the CFLOCKtag around any block of code containing a reference to server variable~ The CFLOCKshould be a ReadOn1y lock. For more information on the CFLOCK tag, please refer to the end of this chapter.
You must always remember to include the server prefix when referring to a server variable. ColdFusion does not automatically evaluate server variables when no prefix is specified. Here are two examples of referring to the server variable server. season:
Using Built-In Server Variables
There are several built-in server variables detailing information about ColdFusion and the server.