环境变量不会从.htaccess传递给PHP [英] Environment variables are not passed from .htaccess to PHP

查看:172
本文介绍了环境变量不会从.htaccess传递给PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个环境变量从.htaccess传递给PHP。这在我的本地WAMP服务器上工作得很好,但是在我的网站托管的服务器上,它无故无故。

I am trying to pass an environment variable from .htaccess through to PHP. This works just fine on my local WAMP server, but on the server where my website is hosted, it fails without reason.

这是我的测试配置:

.htaccess:

.htaccess:

SetEnv TEST_VARIABLE test_value

test.php:

<pre>
getenv('TEST_VARIABLE') = <?php print getenv('TEST_VARIABLE'); ?>

getenv('REDIRECT_TEST_VARIABLE') = <?php print getenv('REDIRECT_TEST_VARIABLE'); ?>
</pre>

在本地服务器上,让test.php正确返回:

On my local server, getting test.php correctly returns:

getenv('TEST_VARIABLE') = test_value
getenv('REDIRECT_TEST_VARIABLE') = 

但在生产服务器上,它返回:

But on the production server, it returns:

getenv('TEST_VARIABLE') = 
getenv('REDIRECT_TEST_VARIABLE') = 

我排除的事情:


  1. mod_env 未被主机安装/启用。不能,因为那个 SetEnv 不会被识别,而在处理.htaccess的时候我会得到500。

  1. mod_env is not installed/enabled by the host. Can't be, because then SetEnv would not be recognized and I'd get a 500 while processing the .htaccess.

该目录的 httpd.conf 中的AllowOverrides 不包括的FileInfo 。不可以,因为当遇到 SetEnv 指令时,Apache会抛出一个错误SetEnv,我再次获得500。

AllowOverrides in httpd.conf for this directory doesn't include FileInfo. Can't be, because then Apache would throw an error "SetEnv not allowed here" when encountering the SetEnv directive and I'd get a 500 again.

variables_order 在php.ini中不包括'E'。这将解释code$ _ ENV superglobal是空的(它是),但不是为什么 getenv()不返回这些变量的值。

variables_order in php.ini doesn't include 'E'. This would explain the $_ENV superglobal being empty (which it is), but not why getenv() doesn't return values for these variables.

整个环境都被搞砸了。不能,因为 getenv('PATH') getenv('SERVER_NAME')仍返回有效值。

Entire environment is screwed up. Can't be, because getenv('PATH') and getenv('SERVER_NAME') still return valid values.

在这一点上,我很遗憾什么配置可能导致这种情况。 b $ b

At this point I'm at a loss as to what configuration could be causing this.

推荐答案

我不认为使用setenv通常在.htaccess中有效。它是一个shell脚本命令(bash,C shell等)。如果您的服务器支持setenv,请根据 https://kb.mediatemple.net/questions/36/Using+Environment+Variables+in+PHP#gs ,您需要使用HTTP _开头的变量名。

I don't think using setenv normally works in .htaccess. It's a shell script command (bash, C shell, etc.). If your server does support setenv, according to https://kb.mediatemple.net/questions/36/Using+Environment+Variables+in+PHP#gs you need to have the variable name start with HTTP_.

这篇关于环境变量不会从.htaccess传递给PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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