First a little background. Application variables are variables used site wide, for example, Site Name, Global email address and settings, URL, etc. They are variables that you would use frequently throughout the site and instead of defining the variable on each page or going to the database, the variable is set as an application variable.

I use Dynamic Application Variables in a CMS I built. When setting up a new site I didn't want to set all the application variables right off the bat then have to go back into the code and change them. And some times I had to change the variables after the site was running so again I didn't want to have to go into the code. Then I would have to have a reset button. Seemed like more of a mess then any thing. So I came up with some code to do it dynamically.

First things second. You will need to define your database connection variables in your code. I put them in my Application.cfc (or the outed .cfm)

The database connection variables are the only one you need manually set, every thing else we will do dynamically. For this tutorial these are the only three we need to set, depending on your database connection string you might need to set others.

We need to create the application variable table in the database.

I named the table "application_variables" with the following fields (app_id, app_name_var, app_var_property, app_var_notes)

<!--- On Submit of the form check if its a new record. First check to see if create_app_var is defined (submit button) Next check if its value is set to insert/create. ---><cfif IsDefined('create_app_var') AND create_app_var EQ "Create Application Variable">

Ok a few things to note here. Hopefully I commented out the code enough so you understand what is going on.

If I'm editing an application variable I'm looking for a url variable or form variable "app_id", that variable comes from the management page that is below.

The next thing is a little trick you might not know. You can dynamically set variable names by quoting them out.

<cfset "application.#app_name_var#" = "#app_var_property#">

So for that code if the app_name_var is email and the property is email@email.com Coldfusion sees it as

<cfset application.email = "email@email.com">

Pretty cool ehy? You can also do this for regular variables.

Now that we have a form to create and edit the application variables lets get on to managing them (view, delete, edit, reset).

<!--- Check to see if we are deleting an App Variable---><cfif IsDefined('submit') AND submit EQ "delete" AND IsDefined('app_id')><!--- First lock the database ---><cflock name="Delete_app_var" type="exclusive" timeout="30"><cftransaction>

<!--- First lets grab the application variable info from the database just to make sure we are deleting the right one ---><!--- We could delete by application variable name but just in case we have more than one with the same name, best to do it by ID ---><cfquery name="getappvari" datasource="#application.dsnodbc#" username="#application.dsnodbc_user#" password="#application.dsnodbc_password#">SELECT app_name_varFROM application_variablesWHERE app_id = <cfqueryparam value="#app_id#" cfsqltype="cf_sql_integer"></cfquery>

<!--- Check to see if we are Setting/Refreshing an App Variable---><cfif IsDefined('submit') AND submit EQ "Set" AND IsDefined('app_id')><!--- First lets grab the application variable info from the database just to make sure we are setting the right one ---><cfquery name="getappvari" datasource="#application.dsnodbc#" username="#application.dsnodbc_user#" password="#application.dsnodbc_password#">SELECT app_name_varFROM application_variablesWHERE app_id = <cfqueryparam value="#app_id#" cfsqltype="cf_sql_integer"></cfquery>

Now lets talk about setting Application variables on the fly. This is for when the server restarts or the application restarts. Instead of manually resetting them we can let the code handle it. And just reset the application variables when they are not found.

There are two ways to do this so please choose the best way for your site.

The first way is if you aren't using a site wide error handler in your application.cfc file. So if you are still old schooling it and using application.cfm or what ever, this will probably be the best way for you.

This code has to be on all pages that might have application variables.

<!--- The page should be wrapped in a cftry. This cftry catches any errors and then checks the error to see if its an application variable error. ---><cftry>

<!--- Check to see if error is an application variable. Catch type is any just in case we get other errors, might as well deal with them too. ---><cfcatch type="any"><!--- Wrap in another cftry just incase some thing doesn't set right. We can dump the user on an error page. ---><cftry>

<!--- If no variable shoot it down and throw the error page. ---><cfif appVariables.recordcount EQ 0><cflocation url="error.cfm" addtoken="no"><cfelse><!--- If we found an application variable. Set it and reload it. ---><cfoutput query="appVariables"><CFSET "application.#cfcatch.element#" = "#var_property#"></cfoutput><cflocation url="#script_name#?#query_string#" addtoken="no"></cfif><!--- If complete error. Throw user to an error page. ---><cfcatch type="any"><cflocation url="error.cfm" addtoken="no"></cfcatch></cftry>

</cfcatch></cftry>

The second way is a bit more complex and requires you to know a bit about the application.cfc file. If you don't, read up about it, Ray Camden, Ben Nadel, and other have some excellent tutorials out there.

I'm not going to post my full Application.cfc file here. It goes beyond the scope of this tutorial. If you are familiar with the application.cfc file you will know what I'm talking about here.

This is just the OnRequest function of the application.cfc file.

<!--- Runs before request as well, after onRequestStart ---><!--- WARNING!!!!! THE USE OF THIS METHOD WILL BREAK FLASH REMOTING, WEB SERVICES, AND AJAX CALLS. DO NOT USE THIS METHOD UNLESS YOU KNOW THIS AND KNOW HOW TO WORK AROUND IT!EXAMPLE: http://www.coldfusionjedi.com/index.cfm?mode=entry&entry=ED9D4058-E661-02E9-E70A41706CD89724---><cffunction name="onRequest" returnType="void"><cfargument name="thePage" type="string" required="true">

<!--- CFTRY to catch any errors on the page, we are looking for expression errors ---><cftry>

<!--- Displays the actual page we are requesting ---><cfinclude template="#arguments.thePage#">

<!--- Catch any variable errors ---><cfcatch type="expression">

<!--- just in case lets look to see if cfcatch.element is defined, it will have the missing variable name ---><cfif IsDefined('cfcatch.element')><!--- Lets check to see if the application variable in the database. ---><cfquery name="AppError" datasource="#application.dsnodbc#" username="#application.dsnodbc_user#" password="#application.dsnodbc_password#">SELECT app_name_var, app_var_propertyFROM application_variablesWHERE app_name_var = '#cfcatch.element#'</cfquery>

<!--- If the variable is in the database... ---><cfif AppError.recordcount NEQ 0><!--- If variable. Set it and load requested page ---><cfoutput query="AppError"><CFSET "application.#cfcatch.element#" = "#app_var_property#"></cfoutput><cfinclude template="#arguments.thePage#">

Both examples are pretty simple concepts, wrap the page in a cftry tag, if error, then check to see if the error is caused by an application variable, if so reset it and display page, If not throw the user to an error page.

I also include the OnError function in my application.cfc just in case the are any other errors.

If you try to catch the application variable errors in the OnError Function, it won't work. It will throw an error, "element is undefined in cfcatch". Its a second level error handler or doesn't directly access the cftry tag? or some thing like that. Not really sure.