为什么我的 $_ENV 是空的? [英] Why is my $_ENV empty?

查看:25
本文介绍了为什么我的 $_ENV 是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 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') 获得它,所以也许我应该改用它.但我还是有点好奇是什么原因造成的.这是 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?

推荐答案

原来这里有两个问题:

1.$_ENV 仅在 php.ini 允许的情况下填充,默认情况下似乎不会这样做,至少在默认情况下不会这样做 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 函数将始终有效,不受 PHP 设置的 $_ENV 影响.此外,它似乎不区分大小写,这可能很有用.

3. The getenv function will always work and is not affected by the PHP setting for $_ENV 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆