为什么我的$ _ENV为空? [英] Why is my $_ENV empty?
问题描述
我正在运行 Apache / 2.2.11(Win32)PHP / 5.3.0
,我在.htaccess文件中执行了以下操作:
I'm running Apache/2.2.11 (Win32) PHP/5.3.0
and I did the following in my .htaccess file:
SetEnv FOO bar
如果我打印出一个PHP文件中的 $ _ ENV
变量,我得到一个空数组。为什么我的环境变量不存在?为什么首先是空的?
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?
我确实找到了我的变量,但它出现在 $ _ SERVER
变量。由于某种原因,它出现了两次。为什么是这样?
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?
[REDIRECT_FOO] => bar
[FOO] => bar
看来我可以使用 getenv('FOO') / code>,所以也许我应该使用它。但是,对于什么原因,我仍然有点好奇。这是Windows的问题吗?或者发生了什么?
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?
推荐答案
结果有两个问题在这里:
Turns out there was two issues here:
1。只有在php.ini允许的情况下才会填充$ _ ENV ,默认情况下似乎不执行,至少不在默认情况下 WAMP服务器安装。
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 server installation.
; 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
不再为空。
When I set the variables_order
back to EGPCS
, $_ENV
is no longer empty.
<强> 2。当您在 .htaccess
中使用 SetEnv
时,最终将以 $ _ SERVER
,而不是 $ _ ENV
,我不得不说,当它命名为 SetEnv
...
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
...
# .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
和 $ _ SERVER
即可。另外,它似乎对case非常敏感,这可能是有用的。
3. The getenv
function worked because it apparently searches both $_ENV
and $_SERVER
. Additionally it seems to do so insensitive to case, which might be useful.
var_dump(getenv('os'), getenv('env'));
// string 'Windows_NT' (length=10)
// string 'dev' (length=3)
这篇关于为什么我的$ _ENV为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!