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

查看:766
本文介绍了为什么我的$ _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('富'),所以也许我应该使用,而不是得到它。但我还是有点好奇,是什么原因导致这一点。这是一个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。 $ _ 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。当您使用 SETENV 的.htaccess ,它在 $结束_ 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 。此外,似乎这样做不敏感的情况下,这可能是有用的。

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屋!

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