Ok, just to start, this is not a post for the seasoned coldfusion developer, this is just a “get an idea of what the heck is going on” sort of post. I was attempting to explain a simple coldfusion login and some different options to a friend, and they were simply not understanding the concept because they didn’t know about application and session variables, so I thought i’d tutorialize this for the others out there who are just confused. Sometimes it’s the simple stuff that’s the hardest to start!
First, you should be at least vaguely familiar with Structures. ok? ok!
There are a few concepts to understand when delving into the world of application and session variables, and i will do my best to avoid using techno talk so you can understand it without having to try too hard. The first, is what an application.cfm (or application.cfc) file is/does.
What an Application.cfm (or application.cfc) file is/does
An application.cfm file is a hunk of code that is executed FIRST before any page you create runs. You MUST name it “application.cfm”. It MUST be at the root of your site (well, not always, but for the basics, yes… must.)
Here is what your 1-page website should look like utilizing an application.cfm:
SITEROOT.com
–index.cfm
–application.cfm
Let’s have a look-see here and show you what this will do.
Here’s my ridiculous application.cfm file:
I'm an application.
And the index:
I'm a sweet index page.
Which will output this:
I'm an application.I'm a sweet index page.
So, we understand. Applications run first, ALWAYS. I’m sure this gets your head turning on the nice things you can now do, but this, my friend, is only the tip of the glorious application file iceberg. There are many, many things you can and should use the application file for, but I don’t want to overwhelm you, so I will just get you excited about application variables.
All right. There are some variables that coldfusion makes available to you that you can use on any page, anywhere, always. These are APPLICATION variables. any variable that you set in the application structure is available anywhere in your website, and is the SAME FOR ALL CLIENTS.
Need to have your datasource name saved? set it!
<cfset application.dsn='mydsn'>Now you can just call your datasource name variable instead of hard coding it in. Why? you ask? because you really, really don’t want people to be able to see your DSN when an error page appears. yeah, not good. they’ll just see the variable name, though if you use the code above!
Mmk, so application variables are for any computer accessing your site, anywhere on your site.
So What is a Session Variable, then?
Unlike Application variables, which (again), once set, can be used by ANY computer, ANY where on your site, Session variables can only be used by ONE computer ANY where on your site. Here is a great example of where session variables would be perfect:
<cfset session.userid=1287>What does the code above do? it sets up a userid that can be used anywhere on the site, but only for the one particular computer accessing your site. This is one way that you could create a secured back end area for different users without having to pass a url or form variable to every page. Maybe you would like to display a person’s name on some pages of your site, now you can! just set it on one page, and then call it when you need it from any other page. as long as the user is still on your site, their name will appear!