我可以配置环境特定内容吗?

发布于 2024-07-28 23:55:22 字数 396 浏览 1 评论 0原文

就像您可以根据浏览器类型生成特定内容一样,有没有一种方法可以根据运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

想你只要分分秒秒 2024-08-04 23:55:22

您可以在 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

甲如呢乙后呢 2024-08-04 23:55:22

我使用以下内容为不同的服务器加载不同的设置:

switch ($_SERVER['SERVER_NAME']) {
case 'web-host': case '10.0.0.208':
    # Set DB Settings
case 'mydomain.com': default:
    # Live server settings
}

到目前为止没有问题

I use the following to load different settings for different servers:

switch ($_SERVER['SERVER_NAME']) {
case 'web-host': case '10.0.0.208':
    # Set DB Settings
case 'mydomain.com': default:
    # Live server settings
}

Not had a problem with it so far

妞丶爷亲个 2024-08-04 23:55:22

在 IIS 上使用 FastCGI,您可以设置环境变量。 它们似乎不可用于 $_ENV,但可以使用 getenv("varname") 检索。

要在 IIS 5 或 6 中配置 FastCGI 环境变量,您需要编辑:
C:\%systemdrive%\system32\inetsrv\fcgiext.ini

例如:

[Types]
php=d:\Dev\PHP\php-cgi.exe
php:1=PHP Site 1
*=Wildcard Mapping

[d:\Dev\PHP\php-cgi.exe]
QueueLength=999
MaxInstances=20
InstanceMaxRequests=500

[PHP Site 1]
ExePath=d:\Dev\PHP\php-cgi.exe
EnvironmentVars=PHPRC:d:\Dev\PHP\,SiteType:Developer

在此实例中,它是 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:

[Types]
php=d:\Dev\PHP\php-cgi.exe
php:1=PHP Site 1
*=Wildcard Mapping

[d:\Dev\PHP\php-cgi.exe]
QueueLength=999
MaxInstances=20
InstanceMaxRequests=500

[PHP Site 1]
ExePath=d:\Dev\PHP\php-cgi.exe
EnvironmentVars=PHPRC:d:\Dev\PHP\,SiteType:Developer

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.

遥远的她 2024-08-04 23:55:22

尚未提及的另一种替代方法是创建一个特定于服务器的(但具有相同名称)配置文件,该文件将包含在站点脚本的开头。 在该服务器特定的配置文件中,您可以将配置变量设置为常量。 这样,如果稍后加载了“通用”配置文件,则其值可能会在特定于服务器的配置文件中被覆盖,因为无法重新定义常量。 您可能希望从同步中排除特定于服务器的配置文件名,或者将其保留在主要内容之外的路径中,以免意外被覆盖。

为此,您甚至可以借助 常量数组 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文