如何在共享主机上使用已编译的 php-cgi 在 .htaccess 中使用 php 中的 getenv() 和 SetEnv
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2008123/
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
How to use getenv() in php and SetEnv in a .htaccess with a compiled php-cgi on a shared host
提问by Steven Rosato
Just putting in context to clarify the main question:
只是在上下文中澄清主要问题:
On my development machine, PHP5.3.1 is installed on Apache as a module, I use SetEnv APPLICATION_ENVIRONMENT development in my application's root .htaccess file. It is then easily retrievable from any php script with getenv('APPLICATION_ENVIRONMENT').
在我的开发机器上,PHP5.3.1 作为模块安装在 Apache 上,我在应用程序的根 .htaccess 文件中使用 SetEnv APPLICATION_ENVIRONMENT 开发。然后可以使用 getenv('APPLICATION_ENVIRONMENT') 从任何 php 脚本中轻松检索它。
On the production server, on a sharedhost (dreamhost), I compiled myself php5.3.1 since it was not directly supported. Everything works fine except that getenv('APPLICATION_ENVIRONMENT') returns false.
在生产服务器上,在共享主机(dreamhost)上,我自己编译了php5.3.1,因为它不直接支持。一切正常,除了 getenv('APPLICATION_ENVIRONMENT') 返回 false。
In the sharedhost root .htaccess for my domain, I use this .htaccess file
在我的域的共享主机根 .htaccess 中,我使用了这个 .htaccess 文件
Options +ExecCGI
AddHandler php-cgi .php
Action php-cgi /cgi-bin/php.cgi
<FilesMatch "^php5?\.(ini|cgi)$">
Order Deny,Allow
Deny from All
Allow from env=REDIRECT_STATUS
</FilesMatch>
Options -indexes
php5.cgi resides in /cgi-bin and works very well. Of course in my application's root folder I have another .htaccess defining:
php5.cgi 位于 /cgi-bin 中并且运行良好。当然,在我的应用程序的根文件夹中,我还有另一个 .htaccess 定义:
SetEnv APPLICATION_ENVIRONMENT production
But when using getenv('APPLICATION_ENVIRONMENT') it returns false, any idea how to resolve this?
但是当使用 getenv('APPLICATION_ENVIRONMENT') 时它返回 false,知道如何解决这个问题吗?
采纳答案by Steven Rosato
Ok I finally got it. On dreamhost, it is possible to use fastcgi and therefore declare environment variables with it. It consists of just adding this simple script
好的,我终于明白了。在 Dreamhost 上,可以使用 fastcgi 并因此用它声明环境变量。它只包括添加这个简单的脚本
#!/bin/sh
export PHP_FCGI_CHILDREN=2
exec /home/USERNAME/YOURDOMAIN/cgi-bin/php.cgi
Which is where my compiled PHP5.3.1 was located. chmod 744 on that file called dispatch.fcgi which will be allowed more memory by dreamhost's watchdog.
这是我编译的 PHP5.3.1 所在的位置。chmod 744 在名为 dispatch.fcgi 的文件上,dreamhost 的看门狗将允许更多内存。
After that I added to my domain's .htaccess the following:
之后,我将以下内容添加到我的域的 .htaccess 中:
Options +ExecCGI
AddHandler fastcgi-script fcg fcgi fpl
AddHandler php5-fastcgi .php
Action php5-fastcgi /dispatch.fcgi
now in the application's root I have another .htaccess with:
现在在应用程序的根目录中,我有另一个 .htaccess:
SetEnv APPLICATION_ENVIRONMENT staging
In a php script is is retrievable via getenv('REDIRECT_APPLICATION_ENVIRONMENT');
在 php 脚本中可以通过 getenv('REDIRECT_APPLICATION_ENVIRONMENT'); 检索;
回答by Pascal MARTIN
For the SetEnvdirective to work, your hosting service must have the mod_envmodule activated...
要使SetEnv指令起作用,您的托管服务必须mod_env激活该模块...
But, even if it's activated, maybe you don't have to permission to use SetEnv.
但是,即使它被激活,也许您也不需要使用SetEnv.
Just to be sure the problem is not in your code, you might want to check the ouput of phpinfo(): at the bottom of the page, there should be a section containing environment variables as seen from PHP -- if yours is not in there, it's not a good sign for you...
只是为了确保问题不在您的代码中,您可能需要检查以下输出phpinfo():在页面底部,应该有一个包含从 PHP 中看到的环境变量的部分——如果您的不在那里,那就是对你来说不是一个好兆头...
回答by Invincibear
In order for $_ENV to work, I had to reconfigure variables_order = "GPCSE"in php.ini. By default, it did not include E for $_ENV, it was originally variables_order = "GPCS"
为了让 $_ENV 工作,我不得不variables_order = "GPCSE"在 php.ini 中重新配置。默认情况下,它没有为 $_ENV 包含 E,它最初是variables_order = "GPCS"
This directive determines which super global arrays are registered when PHP starts up. 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 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";
该指令确定 PHP 启动时注册哪些超级全局数组。G、P、C、E 和 S 分别是以下超级全局变量的缩写:GET、POST、COOKIE、ENV 和 SERVER。注册这些阵列会带来性能损失,并且由于 ENV 不像其他阵列那样常用,因此不建议在生产服务器上使用 ENV。如果需要,您仍然可以通过 getenv() 访问环境变量。
默认值:“EGPCS”
发展价值:“GPCS”
产值:“GPCS”;

