Greg's Blog

helping me remember what I figure out

More Cfapplication Shenanigans

| Comments

The last time I delved into this area was 2 years ago, so I thought it’s about time to revisit this topic and document a few more things I have learned over the years. Before we move on you might want to take a brief side step and have a look at scoping your variables. It’s not integral to further reading, but it does provide a useful insight into variable naming conventions when defining variables in the application.cfm.

Before we delve any further here a few points to be aware off. Firstly you need to know that the application.cfm is always called/parsed first (if found) and then the template that was called by the user. This means that no matter what page your visitor accesses if an application.cfm file exists then it will be processed first by Cold Fusion. This makes it an ideal candidate for storing site wide variables.

When I first visited this topic this is exactly what I suggested. A good example was defining your database variables (username, password, and datasource for example). Now this is not such a good idea as you are exposing some potentially critical information to any person who can view the source of the application.cfm. It is a far better idea to store these within Cold Fusion administrator. However failing that (your ISP may well not give you access to the Cold Fusion administrator), storing those details in the application.cfm makes good sense from a maintenance point of view. Since these variables will be available application wide, they should be scoped as such. So you would defined them as follows:

<!— -Set datasource- —>
<cfset application.dsn = “YourDSN”>
<!— -Set the username- —>
<cfset application.dBUsername = “YourdBUsername”>
<!— -Set the password- —>
<cfset application.dBPassword = “YourdBPassword”>

I may have jumped the gun a bit as the first thing you should do in your application.cfm is define your application name and enable your session management. For those who are a bit unsure as to what session management is, briefly and very much simplified this part allows you to track your site users as they interact with the web site. Session timeouts specified in the application.cfm override the ones set in the Cold Fusion Administrator.

<!— -Declare the application and enable session management- —>
<cfapplication name=”YourApp” sessionmanagement=”Yes” sessiontimeout=”#CreateTimeSpan(0,1,0,0)#”>

The next really useful thing you can do is enable site wide error handling. This is a topic in it’s own right but I’ll give you a brief insight into this area. This is how you enable the site wide error handling:

<!— - If an error occurs re-direct to this page- —>
<cferror type=”REQUEST” template=”error/error.cfm”>

This tag <cferror> with the attribute type set to ”request” is essentially the catch all setting, this means that most, if not all errors cause by your application will result in the the user being re-directed to error/error.cfm. This allows you to display a more user friendly error message than your Cold Fusion debug code or error message.

One thing that I should point out though, is that this is not the most graceful or intelligent way of dealing with errors. Working with <cftry></cftry> and <cfcatch></cfcatch> is far superior approach to error handling. However if you are looking for a quick and easy way to stop those ugly Cold Fusion errors from popping up on your site and confusing users, use <cferror>. Also note that you cannot include any Cold Fusion tags on that page, however you are able to output the following diagnostic variables (just wrap them in between “#”, e.g. #Error.Diagnostics#):

  • Error.Diagnostics
  • Error.MailtTo
  • Error.DateTime
  • Error.Browser
  • Error.GeneratedContent
  • Error.RemoteAddress
  • Error.HTTPReferer
  • Error.Template
  • Error.QueryString

These variables should give you a good indication as to what went wrong and resulted in the error template being called. You may wonder how you could put this information to good use and in the near future I’ll show you a neat trick that will allow you to have this information mailed out to you so that you can track errors on your site and deal with them in your own time.

OK that’s us pretty much done on the topic of application.cfm. Just one or two more things. Application.cfm forms a part of the Cold Fusion application framework and is usually found in the root of your site. However you can have several application.cfm files in your application, these should be kept in sub folders to denote a special set of functionality. The reason for that is that Cold Fusion will work it’s way up through the tree structure of your server until it encounters an application.cfm template. This is important to note, it won’t stop searching at the root of your site, but continue to traverse the tree structure until it hits the root of the drive.

You may be wondering why you would need several application.cfm, for example if you were building an application that required a user to log in, the you could perform that check (i.e. check that a session variable for that user is defined) in the root application.cfm so that it applies to all pages in your application. If the check failed, i.e. the user isn’t logged in, then you could re-direct the user to another folder with an another application.cfm that doesn’t include the log in check, but say still contains all the database connection information. Similarly if you have other sub folders you do not need to specify additional log in checks, because as I already indicated Cold Fusion will work it’s way up through your directory structure until it finds the application.cfm template.

The final thing I’d like to mention is that application.cfm is not the place to keep say a site wide header include, as tempting as it may be. Right that’s it, below is a sample application.cfm template for you to base your applications on.

Code:
<!— -Declare the application and enable session management- —>
<cfapplication name=”YourApp” sessionmanagement=”Yes” sessiontimeout=”#CreateTimeSpan(0,1,0,0)#”>
<!— -Set datasource- —>
<cfset application.dsn = “YourDSN”>
<!— -Set the username- —>
<cfset application.dBUsername = “YourdBUsername”>
<!— -Set the password- —>
<cfset application.dBPassword = “YourdBPassword”>
<!— - If an error occurs re-direct to this page- —>
<cferror type=”REQUEST” template=”error/error.cfm”>