Question

I'm trying to adapt my code to use different database password etc depending on the environment it runs in. For example, I have my dev server, the other developer has his and production env has yet another set of settings.

Since I have some Kohana projects as well, I chose to use the environmental variable KOHANA_ENV for the app to determine where it runs.

The problem is that in my WAMP server, while this works:

<?php echo getenv('REMOTE_ADDR'); ?>

, this doesn't (returns emptyness):

<?php echo getenv('KOHANA_ENV'); ?>

When I type set in the Command Prompt, I see this:

KOHANA_ENV=devbox1

so it should work.

Was it helpful?

Solution

WAMP does not run as the person running wampmanager.exe but rather what the Apache service has been set up to run as. Generally it's LocalService (Local System Account).

http://support.microsoft.com/kb/821761 states that

If you update environment variables or add new environment variables, you must restart the computer before the changes that you make affect services that run under the Local System account.

This behavior occurs because services that run under the Local System account inherit their environment from the Services.exe process. The Services.exe process receives the environment settings for the Local System account when Windows starts. Because the Services.exe process does not use Windows Messaging, when it receives messages that indicate that a value has changed after Windows starts, the Services.exe process does not update its environment settings. You cannot force a service that is running to acknowledge a dynamic change to the environment of the Local System account.

And indeed when the computer is restarted after adding the environmental variable to the System environmental variables, this code:

<?php echo getenv('YOUR_ENVIRONMENTAL_VARIABLE'); ?> 

will print what is expected.

OTHER TIPS

this rings a bell.. try this maybe,

global $HTTP_ENV_VARS;
echo $HTTP_ENV_VARS["KOHANA_ENV"]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top