Solved

Application Variables

Posted on 2011-09-29
8
751 Views
Last Modified: 2013-12-24
Can anyone explain this to me in English?

1.      Static references to server names is restricted the movement of apps across environment.  You do not have to use this (FindNoCase("THESERVERNAME",cgi.server_name) GT 0). It can be replaced by using a global variable that is defined in root application.cfm:
add the following lines of code : <cfinclude template=”/application.cfm”> and then you can use variables.thisServerName (returns servername), variables.thisServerType (returns dev, stage, prod on dev stage and prod environments).

Basically I am having to change what I currently have in my application.cfm to define my variables.  But in the direction given above, I have no clue where to start.

<!--- this is my original code in my application.cfm --->
<!--- Main Variables --->
<CFSCRIPT>
   if (FindNoCase("THESERVERNAME1",cgi.server_name) GT 0) {
      // Development
      variables.appEnvironment = "development";
      variables.appURL = "THESERVERNAME1";
      variables.appRoot = "/thefoldername";
      variables.appScriptRoot = "thefoldername.";
      variables.dbdsn = "mydsn_dev";
      variables.dbuser = "dbusername";
      variables.dbpswd = "dbuserpassword";}
   else if (FindNoCase("THESERVERNAME2",cgi.server_name) GT 0) {
      // Staging
      variables.appEnvironment = "staging";
      variables.appURL = "THESERVERNAME2";
      variables.appRoot = "/thefoldername";
      variables.appScriptRoot = "thefoldername.";
      variables.dbdsn = "mydsn_dev";
      variables.dbuser = "dbusername";
      variables.dbpswd = "dbuserpassword";}
   else if (FindNoCase("THESERVERNAME3",cgi.server_name) GT 0) {
      // Production
      variables.appEnvironment = "production";
      variables.appURL = "THESERVERNAME3";
      variables.appRoot = "/thefoldername";
      variables.appScriptRoot = "thefoldername.";
      variables.dbdsn = "mydsn_prod";
      variables.dbuser = "dbusername";
      variables.dbpswd = "dbuserpassword";}
      variables.appTitle = "My App Title";
   if (!IsDefined("client.last_hit")) {
      client.last_hit = Now();}
</CFSCRIPT>

Open in new window

0
Comment
Question by:Lee R Liddick Jr
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 39

Assisted Solution

by:gdemaria
gdemaria earned 400 total points
ID: 36837821
The person was saying that rather than using a CFIF statement to determine which variables to use, you can set the variables in the application.cfm file on each server as they SHOULD be for that server and just not move the application.cfc file when uploading code.

if (FindNoCase("THESERVERNAME1",cgi.server_name) GT 0) {

This line helps you identify which block of variables to use.  That seems reasonable to me.   The other approach would be to just use the set of variables for each server that you need, without the CFIF statemens.  So on server A you would have these 5 lines and on server B you have a different 5 lines.   In that case, don't copy up your application.cfc file or you will write over your variables.

Either way, I think is fine.

However, the thing to change is the scope of the variables.  
You should be using application scope or at least request scope for these variables.   The "variable" scope should NOT be used for global values!   They are not guaranteed to permiate all files - they are a local variable only.

0
 
LVL 3

Expert Comment

by:sajayc
ID: 36839809
Hi,

An alternative for the line:
if (FindNoCase("THESERVERNAME1",cgi.server_name) GT 0)
could be:
if cgi.servername eq "THESERVERNAME1"

Basically, depending on the server environment that the page is run on it will initialise the variables within the if statement.
Usually variables like Datasource and folder paths change between the different environments.

You don't need the line <cfinclude template=”/application.cfm”> if the above code is already in your application.cfm file.  

But for your understanding, an example where you would use include is below:

<!--- Main Variables --->
<cfif cgi.servername eq "THESERVERNAME1">
      <cfinclude template=”application-dev.cfm”> 
  <cfelseif cgi.servername eq "THESERVERNAME2"> 
      <cfinclude template=”application-stage.cfm”> 
  <cfelseif cgi.servername eq "THESERVERNAME3">
      <cfinclude template=”application-prod.cfm”> 
</cfif>      

Open in new window


Then you have an include file for each of your environments.  But not sure if you really need it.

Hope this helps.
0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 100 total points
ID: 36858119
(no points...gd already gave a good explanation)

> Static references to server names is restricted the movement of apps across environment.

My plain english read on that was:

       Don't hard code server names. It makes the code less flexible. We'd prefer you set
       some global application variables and use those instead of a bunch of hard coded
       cfif statements.



               
0
 
LVL 39

Accepted Solution

by:
gdemaria earned 400 total points
ID: 36891385

> Static references to server names is restricted the movement of apps across environment

I'm not sure I agree with this statement.

The cgi.server_name is the value of the web domain  "www.mywebsite.com"
Based on the current web address, the code is using the correct set of variables.
If your production server went down, you could copy all the code to a backup server and point the domain name to the backup server.  Except for perhaps the folder location, the code would work because it feeds off of the web address.

This code would also prevent your staging or dev websites to ever point against the production database and vise-versa.   For example, what if you accidently copied your application.cfm file from test to production - and you DIDN'T have the code like this.   Your test application.cfm would point your production website to the TEST database... Using the CFIF statements ensure that once the code is copied live, it uses the Live settings.

I think it's a reasonable method to use.

I will reiterate however, that I you need to change the scope of the variables to application (better) or request.

Also, as sajavc suggested, it would probably be better to use "is" or "eq" when testing the cgi.server_name rather than FindNoCase... that would make a more solid match depending on the values...  

Who suggested to do this?
<cfinclude template=”/application.cfm”>

Application.cfm files are executed automatically before any other code, you do not call them directly.   You would not want to put this cfinclude in your code !


Where did all your "advice" come from?   Not sure it's on target...


<CFSCRIPT>
   if (cgi.server_name is "dev.mywebsite.com") {
      // Development
      application.appEnvironment = "development";
      application.appURL = "THESERVERNAME1";
      application.appRoot = "/thefoldername";
      application.appScriptRoot = "thefoldername.";
      application.dbdsn = "mydsn_dev";
      application.dbuser = "dbusername";
      application.dbpswd = "dbuserpassword";}
   else if (cgi.server_name is "staging.mywebsite.com") {
      // Staging
      application.appEnvironment = "staging";
      application.appURL = "THESERVERNAME2";
      application.appRoot = "/thefoldername";
      application.appScriptRoot = "thefoldername.";
      application.dbdsn = "mydsn_dev";
      application.dbuser = "dbusername";
      application.dbpswd = "dbuserpassword";}
   else if (cgi.server_name is "www.mywebsite.com" or cgi.server_name is "mywebsite.com") {
      // Production
      application.appEnvironment = "production";
      application.appURL = "THESERVERNAME3";
      application.appRoot = "/thefoldername";
      application.appScriptRoot = "thefoldername.";
      application.dbdsn = "mydsn_prod";
      application.dbuser = "dbusername";
      application.dbpswd = "dbuserpassword";}
      application.appTitle = "My App Title";
   if (!IsDefined("client.last_hit")) {
      client.last_hit = Now();
      }
</CFSCRIPT>

Open in new window

0
Scale it in WD Gold

With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

 

Author Comment

by:Lee R Liddick Jr
ID: 36891733
Thank you all for your comments as I try to read through them and make sense of it.  The 'advice' comes from company web server administrators.  It is a shared CF environment.  Not sure what difference that makes.  I'm not a CF administrator and never took any CF training.  Been learning as I go...so some of this is way over my head in understanding.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36892902
> what if you accidently copied your application.cfm file from test to production

When you mess up .. things break.  More likely with application.cfm/cfc's of course, but it can happen just as easily happen with other files and settings too.  I'm not disagreeing with what was said.  I'm just more used to shops that don't ever copy over application.cfm/cfc's files.




0
 
LVL 39

Expert Comment

by:gdemaria
ID: 36893002
agreed, that's a best practice.   We have an application.cfc file that is server specific (never copied over), but it extends another cfc file with non-server specific code (login, environment setup, etc).   The extended cfc can be promoted, not the application.cfc
0
 

Author Closing Comment

by:Lee R Liddick Jr
ID: 36893097
As always, thanks gdemaria and aqx for the insight and explanations.  And I know you said no points aqx, but you provided an explanation too.  Spread the love.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now