本文介绍了如何获取PHP 5.3.8读取.user.ini文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台运行Apache 2.2.21和PHP 5.3.8的Mac OS X服务器

I have a Mac OS X server running Apache 2.2.21 and PHP 5.3.8

我想在每个目录的基础上设置一些php.ini样式指令.根据PHP 手册页,自PHP 5.3.0起,通过在每个目录中放置一个名为".users.ini"的文件来覆盖每个目录中的php.ini运行时配置文件中的指令,该文件包含要受影响的脚本.

I would like to set some php.ini style directives on a per-directory basis. According to the PHP manual page, it has been possible since PHP 5.3.0 to override the directives found in the main php.ini runtime configuration file on a per-directory basis by placing a file called ".users.ini" in the directory containing the scripts you want affected.

对我来说,此解决方案比使用.htaccess文件中包含的php_value和php_flag指令更可取,但是我创建的.user.ini文件被PHP忽略.

To me, this solution is highly preferable to using php_value and php_flag instructions contained within a .htaccess file, however the .user.ini file I have created is being ignored by PHP.

根据phpinfo(),我的.user.ini文件应该每5分钟重新缓存一次,并且文件名正确设置为".user.ini":

According to phpinfo(), my .user.ini files should be re-cached every 5 minutes, and the filename is correctly set to ".user.ini":

user_ini.cache_ttl   300
user_ini.filename    .user.ini

Apache设置为AllowOverides,并且我已经确认通过.htaccess文件设置PHP指令正确覆盖了我要控制的ini设置.我尝试过的最简单的测试文件是一个仅包含以下行的.user.ini文件:

Apache is set to AllowOverides, and I have already confirmed that setting the PHP directive via .htaccess file correctly overrides the ini settings I want to control. The simplest test file I have tried is a .user.ini file containing only the line:

display_errors = On

这不起作用,并且使用了系统范围的目录(= Off).但是,等效的.htaccess文件将按预期工作,从而覆盖了系统范围的指令:

This doesn't work and the system-wide directory (=Off) is used. However, the equivalent .htaccess file works as expected, overriding the system-wide directive:

php_flag display_errors on

我可以找到一个线索是主php.ini文件开头的神秘[PHP].我在手册中找不到该节标记的任何提及,因此我认为也许有必要使用[PHP]启动文件.我尝试将其添加到.user.ini文件的开头,然后重新加载了页面,又等待了五分钟,然后再次重新加载,以确保我没有加载缓存的版本,但是不幸的是,它没有任何作用.

One clue I can find is the mysterious [PHP] at the beginning of the main php.ini file. I couldn't find any mention of this section marker in the manual, so I thought perhaps it was necessary to start the file with [PHP]. I tried adding this to the start of my .user.ini file, and reloaded my page, waited another five minutes, reloaded again, to make sure I was not loading a cached version, and unfortunately it made no difference.

我发现的另一个线索是上述PHP 手册页,其中描述了如何使用.user.ini文件:这些文件仅由CGI/FastCGI SAPI处理."不幸的是,除了我的phpinfo()输出中显示"GATEWAY_INTERFACE CGI/1.1"的行外,我不知道如何确认满足此条件,因为我的phpinfo()中实际上没有字符串"cgi"的其他实例) 输出.我只能假设这个神秘的陈述没有多大意义,而只是说明CGI/FastCGI SAPI进行了处理(无论这意味着什么),而不是警告我需要打开某些东西.如果我错了,请纠正我.

The other clue I found is the equally mysterious statement in the aforementioned PHP manual page describing how to use a .user.ini file: "These files are processed only by the CGI/FastCGI SAPI." Unfortunately I don't know how to confirm that this condition is satisfied other than the line in my phpinfo() output which says: "GATEWAY_INTERFACE CGI/1.1" as there is virtually no other instance of the string "cgi" in my phpinfo() output. I am left to assume that this mystery statement is of little importance and is merely stating that the CGI/FastCGI SAPI does the processing (whatever that means), as opposed to warning me that I need to turn something on. Correct me if I'm wrong.

在同一.user.ini手册页的注释中还提到使用php-fpm无法处理.user.ini文件.据我所知,我没有使用它,但是如果有一种简单的检查方法,请告诉我.这确实提出了一个问题,即是否还有其他配置选项或模块与该功能冲突并禁止加载.user.ini文件.有人知道答案吗?

There is also a mention in the comments on the same .user.ini manual page that processing of .user.ini files won't work when using php-fpm. As far as I know, I'm not using that but if there is an easy way to check, let me know. This does raise the question of whether there are other configuration options or modules which clash with the feature and disable the loading of .user.ini files. Anyone know the answer to that?

为了完整起见,我添加了configure命令,以防有人看到一些明显的问题,尽管我认为我并未将其更改为默认值:

For completeness, I include my configure command, in case someone can see some obvious problem, though I don't think I've changed it from the default:

(亲爱的读者,既然您已经阅读了到目前为止,如果您确实发现了与上述配置无关的怪异内容,请在原始帖子的评论中随意提及)

(Since you, dear reader, have read this far, if you do see something weird about the above configuration that's unrelated, feel free to mention it in a comment attached to this original post)

推荐答案

从其他成员的评论中收集了一些线索,我已经能够进行足够的研究以正确回答问题,以供将来参考.

Gathering some clues from other members' comments, I've been able to do enough research to answer the question properly for future reference.

如PHP文档的常规安装注意事项部分所述,PHP可以通过以下两种方式之一与Apache服务器一起运行:

As explained in the General Installation Considerations section of the PHP documentation, PHP can run alongside an Apache server in one of two ways:

  1. 直接在Apache中作为Apache模块(mod_php)也称为SAPI(服务器应用程序编程接口),或者
  2. 作为通过CGI(通用网关接口)或FastCGI协议与Apache通信的独立可执行文件.

当PHP作为Apache中的模块运行(Mac OS X Server上的默认设置)时,PHP无法直接访问文件系统,而Apache提供了PHP完成其工作所需的一切.这意味着不能选择从文件系统获取最新的,按目录的配置选项.这意味着Apache需要负责将每个目录的设置移交给PHP处理器,因此必须通过.htaccess进行设置(Apache处理并传递给PHP)

When PHP runs as a module within Apache (the default setup on a Mac OS X Server), PHP doesn't have direct access to the filesystem, and Apache supplies everything PHP needs to do its job. Which means picking up last-minute, per-directory configuration options from the filesystem is not an option. This means Apache needs to be responsible for handing any per-directory settings to the PHP processor and therefore must be set via .htaccess (which Apache processes and passes to PHP)

当PHP以其自身的进程运行(通过CGI或FastCGI与Apache通信)时,PHP处理器能够在开始处理脚本之前直接访问目录结构,因此可以在其上加载其他配置文件基于目录的基础,这将影响解析错误的显示(无法在脚本运行时进行决定,因为如果脚本无法解析,PHP将无法遵循其中的任何指令).

When PHP runs as it's own process (communicating with Apache via CGI or FastCGI), the PHP processor is able to access the directory structure directly before it starts to process your script and so it is possible to load an additional configuration file on a per-directory basis which would affect things like the displaying of parse errors (impossible to decide at runtime of a script, because if the script doesn't parse, PHP can't follow any instructions therein).

因此,简短的版本是,如果PHP作为Apache模块运行,则需要通过.htaccess文件设置每个目录的配置指令,或切换到通用网关接口.

So the short version is, if PHP is running as an Apache module, you need to set per-directory configuration directives though an .htaccess file, or switch to Common Gateway Interface.

这篇关于如何获取PHP 5.3.8读取.user.ini文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 03:00