php - 为什么我的 $_ENV 是空的?

标签 php apache environment-variables

我正在运行 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/

相关文章:

php - 我的原始 SQL 应该放在 Symfony 2 中的哪里?

Apache 监听配置

apache - 如何在现有 LAMP 服务器上添加 Tomcat 7 虚拟主机?

node.js - 将 # 作为密码存储在 .env 文件中

java - 如何将环境属性读取到xml中

php - 如何获取使用 php/JavaScript 上传的文档第一页的屏幕截图

php - 你能以 "IF"开始 mysql 语句吗?

php - 转换为 HTTPS

php - .htaccess url 编码字符串未正确传递到页面

php - 在 Symfony 2.4 中获取环境变量