views:

371

answers:

3

as it stands i have to maintain a variable in my application.(cfm|cfc) to set the environment, which the application currently runs under. the environment being (development|test|production).

i'd like to set and environment variable on the server itself, so that i can read its value in the application.cfm.

is that possible?

+2  A: 

Using apache you could. In the apache configuration (httpd.conf) or your virtualhost if you have the Env module loaded you can do this:

SetEnv APP_ENVIRONMENT DEVELOPMENT

Then from ColdFusion:

#cgi['APP_ENVIRONMENT']#

If you dump the CGI scope the value will not show, but, it will be there if you output it.

Ian
thanks for the tip. i don't run apache and don't seem to be able to find the setting in iis to do that kind of stuff :/
noobsaibot
this post (from a quick google search) http://forums.iis.net/p/1146779/1858505.aspx suggests it's possible in iis7
Ian
+5  A: 

Easiest is to set a OS environment variable (at the system level, or for the user ColdFusion runs under), and restart the service. The variable is then available in the CGI scope:

<cfset EnvName = CGI.COLDFUSION_ENVIRONMENT>
<cfoutput>#EnvName#</cfoutput>

You could also use Java system properties. In your ColdFusion Administrator, go to "Server Settings/Java and JVM", and add something like this to the "JVM Arguments":

-Dcom.mycompany.environment=development

You can then ask for that value in ColdFusion:

<cfset System  = CreateObject("java", "java.lang.System")>
<cfset EnvName = System.getProperty("com.mycompany.environment")>
<cfoutput>#EnvName#</cfoutput>

You would have to restart the CF Service every time you make a change, but the value seems pretty static so this should not be a problem.

Tomalak
yay, that works. many thanks. btw, how do you guys do that kind of separation?
noobsaibot
Glad to hear it works. :) What do you mean by "separation"?
Tomalak
the separation of code chunks which should be executed only on dev/test/production boxes.
noobsaibot
When editing your post, have a glance to the right (at the help box) and up (at the various editor buttons). ;-)
Tomalak
actually i was talking about the separation in your application.cfm and not in the posting ;)
noobsaibot
Oh. Wood/trees, you know the story. :-D I think this is material for a separate question, not for an out-of-view discussion in some comments. I recommend you abstract the issue into a new question and see what people come up with. :)
Tomalak
I love the jvm approach - I'm currently sniffing details in the server scope, but that's much better - thanks for the tip
Antony
A: 

My first thought on reading the question was to set a SERVER variable:

But then the problem is, where to set that?

In CF9 there'll be a onServerStart() method for this sort of thing.

Adrian Lynch
but then you'd have to maintain that in every application you have, don't you? SETENV and the -D option are set on the *server itself*. no idea how i should have put it different :)
noobsaibot
No, SERVER scoped vars are available to the whole of your ColdFusion server. You might be thinking of the APPLICATION scope.Making it a CGI variable is handy as it'll work across application servers too.
Adrian Lynch