有没有办法查看哪些 php.ini 值具有非默认设置?

Is there a way to see which php.ini values have non-default settings?

我不得不在旧的 PHP 5 服务器上做一些考古。我一直在研究 ini 文件,我突然想到检查哪些设置具有非默认值会非常方便。我发现了 php --iniphp -r 'php_info();' 和其他变体,以及 ini_get_all() function,它可以显示 php ini 文件中设置的值和任何覆盖值(例如来自 .htaccessini_set)。

php.net 文档描述了默认设置 for every ini directive。有没有办法从 PHP 代码内部访问这些默认值?这样我就可以对 ini_get_all 的 return 值进行一些简单的数组操作,并找出哪些具有非默认值。

我正在查看 ini_restore 并且给出的示例看起来好像它只恢复到启动值,即在 ini 文件中配置的值,而不是 php 默认值。

Additionally, you could rename your current ini file to something other than php.ini and restart PHP so that ini_get_all will give you the values which are baked into the core and use parse_ini_file() on your renamed file. – MonkeyZeus

这很有魅力!在开始之前,我有一个链接 conf.d 并且我覆盖了 cli/php.ini 文件以指向 apache2/php.ini 文件,以便我的 php cli 调用将使用 Web 服务器配置。 cli/php.ini 文件已重命名为 .old,如下所示:

$ ls -l /etc/php5/cli/
total 68
lrwxrwxrwx 1 root root     9 Apr 24  2013 conf.d -> ../conf.d
lrwxrwxrwx 1 root root    25 Mar 13 05:04 php.ini -> /etc/php5/apache2/php.ini
-rw-r--r-- 1 root root 67629 Mar  4  2013 php.ini.old

我将 Web 服务器从我们的负载均衡器池中取出并进行了一些修改。

$ rm /etc/php5/cli/conf.d /etc/php5/cli/php.ini
$ php --ini
Configuration File (php.ini) Path: /etc/php5/cli
Loaded Configuration File:         (none)
Scan for additional .ini files in: /etc/php5/cli/conf.d
Additional .ini files parsed:      (none)

然后我添加了一个名为 check_config.php 的文件,其中包含以下内容:

echo("\nDefaults that are changed by or not present in ini file $path:\n");
print_r(array_diff_assoc($defaults, $ini));

echo("\nValues set by $path which differ from or are not included in the defaults:\n");
print_r(array_diff_assoc($ini, $defaults));

并得到了一些美味的输出。

$ /usr/bin/php /etc/php5/cli/check_config.php 

Defaults that are changed by or not present in ini file /etc/php5/apache2/php.ini:
Array
(
    [allow_call_time_pass_reference] => 1
    [allow_url_include] => 0
    //...snip
)

Values set by /etc/php5/apache2/php.ini which differ from or are not included in the defaults:
Array
(
    [engine] => 1
    [asp_tags] => 
    //...snip
)

这做了我想要的,但它有很多转移注意力的东西——很多指令都有默认值但没有包含在 ini 文件中,同样还有很多模块特定的指令不在值中ini_get_all() 返回。我想我可以通过进一步修改配置设置来改进这一点,以启用更多模块,这些模块应该将它们的指令包含在列表中,但是有相当多的模块,所以我认为我现在很好。