我可以配置环境特定内容吗?
就像您可以根据浏览器类型生成特定内容一样,有没有一种方法可以根据运行 PHP 的服务器生成特定内容,而无需引用服务器或站点名称?
例如,PHP 可以自动检测其所处环境并配置数据库连接、ini_set 等错误的方式,具体取决于它是开发、ITS、UAT 还是生产环境。
我想到的两种方法是识别指示开发和 QA 环境的 HTTP 标头,或者在 php.ini 中拥有自定义属性。
我醒了一点,发现 php 函数可以读取 http 标头,但是 php 覆盖了我在 Web 服务器中设置的任何内容,我不知道它们是否可以在 php.ini 中设置。
我不知道是否可以向 php.ini 添加自定义值,但我进行了测试,但 ini_get 找不到它(当然,我在更改 php.ini 后重新启动了 Web 服务器)。
In the same way that you can generate specific content based on browser type is there a way to generate specific content based on the server running PHP without reference to the server or site name?
For example, a way for PHP to automatically detect the environment it was in and configure things like DB connections, ini_set for errors etc. depending if it was a development, ITS, UAT or production environment.
The 2 ways I thought of were to recognise an HTTP header indicating development and QA environments or to have custom properties in php.ini.
I have woken up slightly and found out the php function to read the http headers but php overrides anything I set in the web server and I do not know if they can be set in php.ini at all.
I have no idea if it is possible to add custom values to php.ini but I had a test and ini_get would not find it (I had restarted the web server after changing php.ini of course).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在 apache 中指定一个环境变量(conf、vhost、.htaccess 或作为 httpd daem),然后通过 ^$_ENV^superglobal 访问它
you can specify an environment variable in apache (conf, vhost, .htaccess or as an httpd daem) and then acces it via the ˆ$_ENVˆsuperglobal
我使用以下内容为不同的服务器加载不同的设置:
到目前为止没有问题
I use the following to load different settings for different servers:
Not had a problem with it so far
$_ENV / http://www.php.net/manual/en /reserved.variables.environment.php
$_ENV / http://www.php.net/manual/en/reserved.variables.environment.php
在 IIS 上使用 FastCGI,您可以设置环境变量。 它们似乎不可用于 $_ENV,但可以使用 getenv("varname") 检索。
要在 IIS 5 或 6 中配置 FastCGI 环境变量,您需要编辑:
C:\%systemdrive%\system32\inetsrv\fcgiext.ini
例如:
在此实例中,它是 IIS 5,因此只有一个站点,站点 ID 为 1,如 [Types] 第 2 行所示。
在 IIS 6 上,您可能有多个站点,以下链接告诉您如何查找站点 ID:http://weblogs.asp.net/owscott/archive/2005/07/29/how-to-find-the -siteid-in-iis5-and-iis6.aspx。
一旦安装了 IIS 7 的管理包,显然就可以通过 UI 来配置 IIS 7。
Using FastCGI on IIS you can set Environment variables. They do not seem to be available to $_ENV but can be retrieved with getenv("varname").
To configure FastCGI environment variables in IIS 5 or 6 you need to edit:
C:\%systemdrive%\system32\inetsrv\fcgiext.ini
For example:
In this instance it is IIS 5 so there is only one site and the site ID is 1 as indicated in line 2 of [Types].
On IIS 6 you may have multiple sites and the following link tells you how to find the Site ID: http://weblogs.asp.net/owscott/archive/2005/07/29/how-to-find-the-siteid-in-iis5-and-iis6.aspx.
IIS 7 can be configured via the UI apparently once the Administration Pack for IIS 7 has been installed.
尚未提及的另一种替代方法是创建一个特定于服务器的(但具有相同名称)配置文件,该文件将包含在站点脚本的开头。 在该服务器特定的配置文件中,您可以将配置变量设置为常量。 这样,如果稍后加载了“通用”配置文件,则其值可能会在特定于服务器的配置文件中被覆盖,因为无法重新定义常量。 您可能希望从同步中排除特定于服务器的配置文件名,或者将其保留在主要内容之外的路径中,以免意外被覆盖。
为此,您甚至可以借助 常量数组 2 类。
当使用特定于服务器的配置文件时,您不必担心当前的 SERVER_NAME - 这使您可以更轻松地定义预期环境,而不管当前的系统名称如何,这对于 QA 目的也很方便。
Another alternative that hasn't been mentioned yet would be to create a server-specific (but with the same name) configuration file that would be included in the beginning of your site script. In that server-specific config file you could set configuration variables as constants. That way, if there was a 'generic' configuration file loaded later, its values could be overridden in the server-specific configuration file as constants can't be redefined. You would want to either exclude the server-specific configuration file name from the synchronizations, or keep it in a path outside of the main content so that it is not accidentally overwritten.
For this purpose you can even configure Constant Arrays with help of Constant Array 2 class.
When server-specific configuration files are used you don't have to worry about the current SERVER_NAME -- this makes it easier for you to define the intended environment regardless of the current system name which could be handy also for QA purposes.