putenv/getenv, $_ENV, and phpinfo(INFO_ENVIRONMENT) are three completely distinct environment stores. doing putenv(“x=y”) does not affect $_ENV; but also doing $_ENV[“x”]=”y” likewise does not affect getenv(“x”). And neither affect what is returned in phpinfo().
putenv(); Adds setting
to the server environment. The environment variable will only exist for the duration of the current request. At the end of the request the environment is restored to its original state.
Assuming the USER environment variable is defined as “dave” before running the following:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php print "env is: ".$_ENV["USER"]."\n"; print "(doing: putenv fred)\n"; putenv("USER=fred"); print "env is: ".$_ENV["USER"]."\n"; print "getenv is: ".getenv("USER")."\n"; print "(doing: set _env barney)\n"; $_ENV["USER"]="barney"; print "getenv is: ".getenv("USER")."\n"; print "env is: ".$_ENV["USER"]."\n"; phpinfo(INFO_ENVIRONMENT); ?> |
prints:
env is: dave
(doing: putenv fred)
env is: dave
getenv is: fred
(doing: set _env barney)
getenv is: fred
env is: barney
phpinfo()
Environment
Variable => Value
…
USER => dave
…