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

问题描述

我试图通过对PHP传递从的.htaccess的环境变量。这工作我的本地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的:

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') =

事情我已经排除了:

Things I've ruled out:


  1. mod_env 未安装/主机启用。不能,因为那时 SETENV 将不被认可,我会得到一个500,而处理的.htaccess。

  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.

AllowOverrides 的httpd.conf 此目录不包括的FileInfo 。不能,因为那时的Apache将抛出一个错误SETENV这里不允许遇到 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'。这可以解释的 $ _ ENV 超全局是空的(它是),但不知其所以然的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('路径') GETENV('SERVER_NAME')还是回到有效值。

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

在这一点上我很茫然,以什么样的配置可能会导致这。

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

推荐答案

我不认为使用SETENV正常工作在.htaccess。这是一个shell脚本命令(bash中,C壳等)。如果您的服务器的确实的支持SETENV,根据的你需要有变量名与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传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:23