Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Application Variables

Posted on 2011-09-29
8
Medium Priority
?
785 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 1600 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 400 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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
LVL 39

Accepted Solution

by:
gdemaria earned 1600 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
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
What You Need to Know when Searching for a Webhost Provider
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

885 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