• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 794
  • Last Modified:

Application Variables

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
Lee R Liddick Jr
Asked:
Lee R Liddick Jr
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
gdemariaCommented:
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
 
sajaycCommented:
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
 
_agx_Commented:
(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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
gdemariaCommented:

> 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
 
Lee R Liddick JrReporting AnalystAuthor Commented:
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
 
_agx_Commented:
> 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
 
gdemariaCommented:
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
 
Lee R Liddick JrReporting AnalystAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now