php 为什么我的 $_ENV 是空的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3780866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my $_ENV empty?
提问by Svish
I'm running Apache/2.2.11 (Win32) PHP/5.3.0
and I did the following in my .htaccess file:
我正在运行,Apache/2.2.11 (Win32) PHP/5.3.0
并且在我的 .htaccess 文件中执行了以下操作:
SetEnv FOO bar
If I print out the $_ENV
variable in a PHP file, I get an empty array. Why doesn't my environment variable appear there? Why is it empty in the first place?
如果我打印出$_ENV
PHP 文件中的变量,我会得到一个空数组。为什么我的环境变量没有出现在那里?为什么它首先是空的?
I did find my variable though, but it appears in the $_SERVER
variable. And for some reason it appears twice, sort of. Why is this?
我确实找到了我的变量,但它出现在$_SERVER
变量中。出于某种原因,它出现了两次,有点。为什么是这样?
[REDIRECT_FOO] => bar
[FOO] => bar
It appears I can get it using getenv('FOO')
, so maybe I should just use that instead. But I am still a bit curious to what causes this. Is this a Windows issue? Or what is going on?
看来我可以使用它getenv('FOO')
,所以也许我应该使用它。但我还是有点好奇是什么原因造成的。这是 Windows 的问题吗?或者是怎么回事?
回答by Svish
Turns out there was two issues here:
结果发现这里有两个问题:
1. $_ENV
is only populated if php.ini allows it, which it doesn't seem to do by default, at least not in the default WAMP serverinstallation.
1.$_ENV
仅在 php.ini 允许的情况下填充,默认情况下似乎不会这样做,至少在默认的WAMP 服务器安装中不会。
; 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"
When I set the variables_order
back to EGPCS
, $_ENV
is no longer empty.
当我将variables_order
返回设置为 时EGPCS
,$_ENV
不再是空的。
2. When you use SetEnv
in your .htaccess
, it ends up in $_SERVER
, not in $_ENV
, which I gotta say is a tad confusing when it's named SetEnv
...
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. The getenv
function will always work and is not affected by the PHP setting for $_ENVAdditionally it seems to do so insensitive to case, which might be useful.
3. 该getenv
函数将始终有效,不受 PHP 设置 $_ENV 的影响此外,它似乎对大小写不敏感,这可能很有用。
var_dump(getenv('os'), getenv('env'));
// string 'Windows_NT' (length=10)
// string 'dev' (length=3)
回答by NullUserException
$_ENV
variables are imported from the environment under which PHP is running, and depending on your setup (the OS, your server, whether PHP runs as an Apache module or under FastCGI, etc.), this can vary greatly.
$_ENV
变量是从 PHP 运行的环境中导入的,并且根据您的设置(操作系统、服务器、PHP 是作为 Apache 模块运行还是在 FastCGI 下运行等),这可能会有很大差异。
IIRC in a standard Apache+mod_php install on Windows, the only way to change variables in $_ENV
is to change Windows' environment variables (see this). This can be significant when dealing with PHP extensions on Windows, because some of them (eg: php_ldap
) are only configurable through environment vars on $_ENV
.
IIRC 在 Windows 上的标准 Apache+mod_php 安装中,更改变量的唯一方法$_ENV
是更改 Windows 的环境变量(请参阅此)。这在处理 Windows 上的 PHP 扩展时非常重要,因为其中一些(例如php_ldap
:)只能通过环境变量 on 进行配置$_ENV
。
回答by mario
REDIRECT_*
variables appear if you are using RewriteRules. On my server they also appear just so. It might have something to do with running under FastCGI. And if combined with suexec, that's most likely to clean up the complete environment var pool. There might be additional configuration necessary to get them back, PassEnv
particularily. As to why getenv() works for you, I have no clue. But all phenomena are specific to your server and php configuration. Ask on serverfault, they should know.
REDIRECT_*
如果您使用 RewriteRules,则会出现变量。在我的服务器上,它们也是如此。它可能与在 FastCGI 下运行有关。如果与 suexec 结合使用,则最有可能清理整个环境变量池。可能需要额外的配置才能让它们恢复,PassEnv
特别是。至于为什么 getenv() 对你有用,我不知道。但是所有现象都特定于您的服务器和 php 配置。问serverfault,他们应该知道。