Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Coldfusion Application and request scope

Posted on 2012-08-26
10
Medium Priority
?
1,135 Views
Last Modified: 2012-08-26
Hello experts.
I have a question about setting variables in Application or request scope.
Most of them are in the application scope f.e
<cfset application.homeURL = 'myhomeurl' />
<cfset application.secureURL = "mysecureurl"/>
<cfset application.staticURL = "myurl for css , js and image files" />
<cfset application.absolutepath = "/"/>
<cfset application.bslash = '\'>
<cfset application.catalogpath = "#expandpath('#application.absolutepath#')#"/>
<cfset application.imagespath = expandpath("images")/>
<cfset application.AdminPath = '#application.absolutepath#admin/'>

Now there are some settings that the administrator of a website can change:
f.e.
<cfset application.PASSWORDSITE = false > <!--- Setting login to the site or not --->
<cfset application.STATS_ENABLED = true > <!--- enable or not the statistics ---->
<cfset application.USE_URLREWRITE = false >
<cfset application.showonlineusers = true />
<cfset application.maximagesupload = 10 />

I could use the application scope for these and after making a change ,restart the application OR use the request scope

Finally i have read a lot of using the database name in application or request scope but i can't deside what to use.
<cfset application.dsn = "my database name"/>

Because i don't know wich hoster i will use for my websites (i 'm using now a hoster supporting cf9) i need your opinion about my setup having in mind that i will use CF8 +
0
Comment
Question by:Panos
  • 5
  • 3
  • 2
10 Comments
 
LVL 4

Accepted Solution

by:
TechHelpr08210 earned 2000 total points
ID: 38334343
Generally speaking you setup seems ok for CF 8.

The good rule of thumb for when to add variable into the application scope is to think about how often the scoped variable's value updated. If your answer is that the variable is only set once or twice during the life span of the application, you have a variable perfect for the appliaction scope.

With that said, you may also want to consider using application.cfc instead of Application.cfm. The file application.cfc is generally considered a better way to set application variables as compared to the older and more confining application.cfm include style. application.cfc is will gives you more control over exactly when the application variables will load.

When the Application.cfc file runs for the first time, these methods are called in the order as shown in the following diagram. The request variable scope is available at all times. Yet, to make the code flow right, the designers of this object made sure of the order in which the server runs the code. You will also find that for technical reasons, there are some issues that arise when we use the onRequest() method. Therefore, we will not be using this method. Click Here to view graphic

Original Page Reference: Click Here


ColdFusion MX 7 introduced the Application.cfc ColdFusion component. This component replaces the traditional Application.cfm and OnRequestEnd.cfm ColdFusion application templates. Furthermore, if Application.cfc is present, both of these templates are ignored by the application.

In addition to replacing the request start/end functionality of the old application templates, Application.cfc provides hooks into many additional application level events including:

OnApplicationStart()
This fires when the application first runs. It is a single-threaded method call.

OnSessionStart()
This fires when an individual session first runs. It is a single-threaded method call.

OnRequestStart()
This fires when a page request first runs. It is a single-threaded method call.

OnRequest()
This fires when the requested template needs to get processed.

OnRequestEnd()
This fires at the end of every page request. It is a single-threaded method call.

OnSessionEnd()
This fires at the end of every session. This will fire either for a session timeout or if the server is shutting down. It is a single-threaded method call.

OnApplicationEnd()
This fires at the end of the application. This will fire for an application timeout of if the server is shutting down. It is a single-threaded method call.

OnError()
This fires if an exception gets thrown and is not caught by the controlling code.

All of the above methods are optional. You have an Application.cfc that is completely empty. Also, I say that the above methods are single-threaded which means that you do not have to worry about locking the code within them. However, any and all of these methods can be called explicitly by other parts of the application (ex. calling OnApplicationStart() from OnRequestStart() for manually fired application re-initialization). In this case, the manually invoked events are NOT single-threaded and may be open to race conditions.

In addition to application event hooks, Application.cfc provides public properties that define the application in a manner similar to the CFApplication tag. The following are available THIS-scoped properties:

THIS.Name
This is the name of the application and is used to tie a request to existing application memory scopes.

THIS.ApplicationTimeout
The time span an application will exist before it times out (if the application is not accessed in any way). This defaults to the value set in the ColdFusion administrator.

THIS.SessionManagement
A boolean flag to determine if the SESSION scope should be used. This defaults to false.

THIS.SessionTimeout
The time span a session will exist before it times out (if the application is not access in any way by that session's user). This defaults to the value set in the ColdFusion administrator.

THIS.SetClientCookies
A boolean flag to determine if the CFID and CFTOKEN values are sent as cookies to the client's browser. This defaults to true.

THIS.SetDomainCookies
A boolean flag to determine if the CFID and CFTOKEN values are set for domain and not just a host value. This defaults to false. I have never used this and so I am not exactly sure what this means.

THIS.ClientManagement
A boolean flag to determine if the CLIENT scope should be used. This defaults to the value set in the ColdFusion administrator.

THIS.ClientStorage
If client management is being used, this determines where the client variables are being stored (cooke, registry, or database). This defaults to the value set in the ColdFusion administrator.

THIS.LoginStorage
The place login information is stored (cookie or session). I have never used this, so I am not exactly sure what that means. Defaults to cookie.

THIS.ScriptProtect
A boolean flag to determine if the variables will be protected from cross-site-scripting attacks. Again, I have never used this and I am not sure what it does. This defaults to the value set in the ColdFusion administrator.

You may want to read the rest of this article about using application.cfc. I have personally found all of his articles to be very well written. Article Found Here



Putting it all together, an Application.cfc ColdFusion component skeleton would look something like this:

<cfcomponent
displayname="Application"
output="true"
hint="Handle the application.">
 
<!--- Set up the application. --->
<cfset THIS.Name = "AppCFC" />
<cfset THIS.ApplicationTimeout = CreateTimeSpan( 0, 0, 1, 0 ) />
<cfset THIS.SessionManagement = true />
<cfset THIS.SetClientCookies = false />
 
<!--- Define the page request properties. --->
<cfsetting
requesttimeout="20"
showdebugoutput="false"
enablecfoutputonly="false"
/>

<cffunction
name="OnApplicationStart"
access="public"
returntype="boolean"
output="false"
hint="Fires when the application is first created.">
 
<!--- Return out. --->
<cfreturn true />
</cffunction>
 

 

<cffunction
name="OnSessionStart"
access="public"
returntype="void"
output="false"
hint="Fires when the session is first created.">

<!--- Return out. --->
<cfreturn />
</cffunction>

<cffunction
name="OnRequestStart"
access="public"
returntype="boolean"
output="false"
hint="Fires at first part of page processing.">

<!--- Define arguments. --->
<cfargument
name="TargetPage"
type="string"
required="true"
/>

<!--- Return out. --->
<cfreturn true />
</cffunction>

<cffunction
name="OnRequest"
access="public"
returntype="void"
output="true"
hint="Fires after pre page processing is complete.">

<!--- Define arguments. --->
<cfargument
name="TargetPage"
type="string"
required="true"
/>

<!--- Include the requested page. --->
<cfinclude template="#ARGUMENTS.TargetPage#" />

<!--- Return out. --->
<cfreturn />
</cffunction>

<cffunction
name="OnRequestEnd"
access="public"
returntype="void"
output="true"
hint="Fires after the page processing is complete.">

<!--- Return out. --->
<cfreturn />
</cffunction>

<cffunction
name="OnSessionEnd"
access="public"
returntype="void"
output="false"
hint="Fires when the session is terminated.">

<!--- Define arguments. --->
<cfargument
name="SessionScope"
type="struct"
required="true"
/>

<cfargument
name="ApplicationScope"
type="struct"
required="false"
default="#StructNew()#"
/>
 
<!--- Return out. --->
<cfreturn />
</cffunction>
 
<cffunction
name="OnApplicationEnd"
access="public"
returntype="void"
output="false"
hint="Fires when the application is terminated.">

<!--- Define arguments. --->
<cfargument
name="ApplicationScope"
type="struct"
required="false"
default="#StructNew()#"
/>
 
<!--- Return out. --->
<cfreturn />
</cffunction>
 
<cffunction
name="OnError"
access="public"
returntype="void"
output="true"
hint="Fires when an exception occures that is not caught by a try/catch.">

<!--- Define arguments. --->
<cfargument
name="Exception"
type="any"
required="true"
/>

<cfargument
name="EventName"
type="string"
required="false"
default=""
/>

<!--- Return out. --->
<cfreturn />
</cffunction>

</cfcomponent>

Open in new window

0
 
LVL 2

Author Comment

by:Panos
ID: 38334427
Hi TechHelpr08210
Thank you for the detailed description.
I'm using Application.cfc and i'm using a cfinclude .cfm file with my application variables where i'm storing my changes using my admin panel.

What about the database? Store in application or request scope?
(For Cf8 - 9 ....)
0
 
LVL 4

Assisted Solution

by:TechHelpr08210
TechHelpr08210 earned 2000 total points
ID: 38334517
To answer your question, you should always use the Application scope for setting a DSN value and any security access information needed to login.

Unless you were running the application on a clustered server environment, then you might want to use the server scope instead.

TC
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:Panos
ID: 38334600
Hmmmm.
I did read some  articles like this one :
http://bangalorecfug.groups.adobe.com/index.cfm?event=post.display&postid=10513
and because i did not understand exactly what issue he is describing i got a little confused.
0
 
LVL 4

Assisted Solution

by:TechHelpr08210
TechHelpr08210 earned 2000 total points
ID: 38334729
I read the article and understand how that could have been your take away. However, the generall issue with shared scope variables described in the article is mentioned in the best practices document by Adobe has having been corrected.

Additionally, the author mentioned issues in regard to not having access to the application scope within customtags when the application variables are set inside of an application.cfm file. However, setting variables inside the onapplicationstart function inside of application.cfc is very different at least in how it treats the variables in memory.

Check out this reference: Adobe best practices for locking
0
 
LVL 2

Author Comment

by:Panos
ID: 38334736
Thank you very much for the clarification
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 38334737
Thank you
regards
panos
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38335191
Respectfully, I disagree with always using the application scope.  You need to be careful if you're on shared hosting, because your app variables can be exposed to others on the server by guessing your app name.  If you're just storing file paths it may not matter. But if you're storing sensitive info like passwords, be sure to factor that aspect into your decision too.

http://jochem.vandieten.net/tag/application-scope/
0
 
LVL 2

Author Comment

by:Panos
ID: 38335524
Hi agx.
Thank you very much for this interesting post.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38335538
You're welcome.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question