Monday, April 12, 2010

ColdFusion: Share Variables Among Multiple Application Files

A classic case of over thinking...

The scenario: I am creating a simple tracking system in ColdFusion that follows a user to each of the different services my site offers. I had an application.cfm file off of the root of the site. This application file grabs the user's basic information through the log in. Part of that basic information is a unique number that identifies the user. It was this unique number that I was going to use to 'follow' the user from subdirectory to subdirectory.

My initial thought was to simply put an application.cfm file in each of the subdirectories that the user would go to and it would write a record to a database. The problem, I kept getting a 'variable is undefined' error whenever I would try to call up the unique number in the subdirectory's application.cfm file. I took my late afternoon on Friday and this morning mad searching the Internet to try to figure out how to do this. I came up with all of these elaborate methods of parsing the variable. I tried making a session variable, a URL variable, and setting it as a cookie. Each one ran into the same error.

All of my searching came up fruitless. "No one had this issue before," I thought beginning to get frustrated. While I was writing this little system, I was also responding to e-mails from someone I was training some of the basics of ColdFusion. In one of the e-mails I gave a quick synopsis of my frustration. Her answer was a simple, "why don't you just do an include of the root application.cfm file in the other application files?"

It was brilliant in its simplicity. Sure enough, it worked like a charm. Using a virtual path lets me put the application.cfm in each of the different directories. I was ruined by a classic case of over thinking. I am sure every ColdFusion developer who is reading this is thinking, "yeah, you're a genius, I would have thought of that within seconds." Bbut in case there is anyone else running into this issue in the world, there is now one page on the Internet that will come up when running the search and you may avoid a hair of frustration.

No comments:

Post a Comment