我正在运行 Apache/2.2.11 (Win32) PHP/5.3.0
,我在 .htaccess 文件中执行了以下操作:
SetEnv FOO bar
如果我在 PHP 文件中打印出 $_ENV
变量,我会得到一个空数组。为什么我的环境变量没有出现在那里?为什么它首先是空的?
我确实找到了我的变量,但它出现在 $_SERVER
变量中。出于某种原因,它出现了两次,有点。这是为什么呢?
[REDIRECT_FOO] => bar
[FOO] => bar
看来我可以使用 getenv('FOO')
来获取它,所以也许我应该改用它。但我还是有点好奇是什么原因造成的。这是 Windows 问题吗?或者是怎么回事?
最佳答案
原来这里有两个问题:
1. $_ENV
仅在 php.ini 允许时才填充,默认情况下它似乎不会这样做,至少在默认情况下 WAMP server 不会这样做安装。
; This directive determines which super global arrays are registered when PHP
; starts up. If the register_globals directive is enabled, it also determines
; what order variables are populated into the global space. G,P,C,E & S are
; abbreviations for the following respective super globals: GET, POST, COOKIE,
; ENV and SERVER. There is a performance penalty paid for the registration of
; these arrays and because ENV is not as commonly used as the others, ENV is
; is not recommended on productions servers. You can still get access to
; the environment variables through getenv() should you need to.
; Default Value: "EGPCS"
; Development Value: "GPCS"
; Production Value: "GPCS";
; http://php.net/variables-order
variables_order = "GPCS"
当我设置 variables_order
返回 EGPCS
, $_ENV
不再为空。
<强>2。当您使用 SetEnv
在您的.htaccess
, 它以 $_SERVER
结束,不在 $_ENV
中,当它被命名为 SetEnv
时,我得说有点令人困惑。 ...
# .htaccess
SetEnv ENV dev
SetEnv BASE /ssl/
# php
var_dump($_SERVER['ENV'], $_SERVER['BASE']);
// string 'dev' (length=3)
// string '/ssl/' (length=5)
3. getenv
函数将始终有效,并且不受 $_ENV 的 PHP 设置的影响 此外,它似乎对大小写不敏感,这可能很有用。
var_dump(getenv('os'), getenv('env'));
// string 'Windows_NT' (length=10)
// string 'dev' (length=3)
关于php - 为什么我的 $_ENV 是空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3780866/