noobsaibot
noobsaibot

Reputation: 377

How to set a server-wide variable in ColdFusion

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 an environment variable on the server itself, so that I can read its value in the Application.cfm.

Is that possible?

Upvotes: 7

Views: 5835

Answers (3)

Adrian Lynch
Adrian Lynch

Reputation: 8494

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.

Upvotes: 0

Tomalak
Tomalak

Reputation: 338188

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.

Upvotes: 11

Ian
Ian

Reputation: 1622

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.

Upvotes: 3

Related Questions