在 httpd.conf 中设置 PHP 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/146354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting PHP variables in httpd.conf?
提问by Polsonby
I'd like to automatically change my database connection settings on a per-vhost basis, so that I don't have to edit any PHP code as it moves from staging to live and yet access different databases. This is on a single dedicated server.
我想在每个虚拟主机的基础上自动更改我的数据库连接设置,这样我就不必编辑任何 PHP 代码,因为它从登台转移到实时访问不同的数据库。这是在单个专用服务器上。
So I was wondering, can I set a PHP variable or constant in httpd.conf as part of the vhost definition that the site can then use to point itself to a testing database automatically?
所以我想知道,我可以在 httpd.conf 中设置一个 PHP 变量或常量作为 vhost 定义的一部分,然后站点可以使用它自动将自己指向测试数据库吗?
$database = 'live';
if (some staging environment variable is true) {
$database = 'testing'; // and not live
}
If this isn't possible, I guess in this case I can safely examine the hostname I'm running on to tell, but I'd like something a little less fragile
如果这是不可能的,我想在这种情况下我可以安全地检查我正在运行的主机名,但我想要一些不那么脆弱的东西
Hope this makes sense
希望这是有道理的
many thanks
非常感谢
Ian
伊恩
采纳答案by Gravstar
Did you tried to use the .htaccess file? You could override the php.ini values using it.
您是否尝试使用 .htaccess 文件?您可以使用它覆盖 php.ini 值。
Just put the .htaccess file into your htdocs directory:
只需将 .htaccess 文件放入您的 htdocs 目录:
php_value name value
Futher information:
更多信息:
回答by JW.
Yep...you can do this:
是的……你可以这样做:
SetEnv DATABASE_NAME testing
and then in PHP:
然后在 PHP 中:
$database = $_SERVER["DATABASE_NAME"];
or
或者
$database = getenv("DATABASE_NAME");
回答by Christian Lescuyer
You can set an environment variable and retrieve it with PHP.
您可以设置环境变量并使用 PHP 检索它。
In httpd.conf:
在 httpd.conf 中:
SetEnv database testing
In your PHP:
在您的 PHP 中:
if (getenv('database') == 'testing') {
or
或者
if ($_SERVER['database'] == 'testing') {
回答by SchizoDuckie
I would not set an environment variable, as this is also visible in default script outputs like PhpInfo();
我不会设置环境变量,因为这在 PhpInfo() 等默认脚本输出中也是可见的;
just use a php_value in your .htaccess just above the htdocs folder and you're done and safe :)
只需在 htdocs 文件夹上方的 .htaccess 中使用 php_value 即可完成并安全:)
回答by nitish
The problem with .htaccess is that it is part of the code base tree. And the code base tree is part of VC/SVN. Hence any change in local/dev gets moved to production. Keeping the env variable setting in httpd.conf saves you the effort of being careful about not accidentally overwriting the server vs dev flag. Unless of course you want to do with IP address or host name, both of which are not scalable approaches.
.htaccess 的问题在于它是代码库树的一部分。代码库树是 VC/SVN 的一部分。因此,本地/开发中的任何更改都会转移到生产中。在 httpd.conf 中保留 env 变量设置可以节省您小心不要意外覆盖服务器与开发标志的努力。除非您当然想使用 IP 地址或主机名,否则这两者都不是可扩展的方法。
回答by S.Donovan
I was also looking at this type of solution. What I found is this, under Apache you can use the SetEnv KeyName DataValuein the http.conf and in IIS you can use Fast CGI Settings >> Edit... >> Environment Variables >> ... and add KeyName, DataValue.
我也在研究这种类型的解决方案。我发现的是,在 Apache 下,您可以SetEnv KeyName DataValue在 http.conf 中使用 ,而在 IIS 中,您可以使用 Fast CGI Settings >> Edit... >> Environment Variables >> ... 并添加KeyName, DataValue.
This in turn allows the PHP $var = $_SERVER["KeyName"];to be set to the DataValueand used as needed under both IIS and Apache consistently.
这反过来又允许 PHP$var = $_SERVER["KeyName"];设置为DataValueIIS 和 Apache 下根据需要一致地使用。
I know this is a strange use case. I use WAMP at work and MAMP at home so it is nice to be able to work the same way.
我知道这是一个奇怪的用例。我在工作中使用 WAMP,在家里使用 MAMP,因此能够以相同的方式工作真是太好了。

