PHP putenv()没有更新

On my apache instance it is setting an env variable APP_ENV=development. I am trying to change this dynamically on my PHP side (in the instance of firing up test suite) like so:

putenv('APP_ENV=testing')
var_dump(getenv('APP_ENV')); // still returns development

I have tried:

  • Starting php in safe mode in php.ini
  • Setting safe_mode_allowed_env_vars = PHP_ APP_ in php.ini

Update:

I am using PHP version 5.4.16 and notice that safe mode has been deprecated. I'm not sure if this means putenv will even work for overwriting or even unsetting existing envs?

You are using an Apache variable, so, you should use apache_setenv() and apache_getenv()

apache_setenv('APP_ENV', 'testing');

To recover it use:

apache_getenv('APP_ENV');

The docs say the list needs to be comma delimited.

try PHP_,APP_