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

问题描述:

I've had to do some archeology on an old PHP 5 server. I've been digging around in the ini files and it occurred to me that it would be very convenient to check which settings have non-default values. I've discovered php --ini and php -r 'php_info();' and other variants, as well as the ini_get_all() function, which can show the value set in the php ini files and any overridden value (e.g. from .htaccess or ini_set).

The php.net documentation describes a default setting for every ini directive. Is there a way to access these defaults from inside PHP code? That way I could do some simple array manipulations on the return value of ini_get_all and pick out which ones have non-default values.

I was looking at ini_restore and the example given reads as though it only restores to the startup value, i.e. the value configured in the ini files, not the php default value.

我不得不在旧的PHP 5服务器上做一些考古学。 我一直在ini文件中挖掘,我发现检查哪些设置具有非默认值非常方便。 我发现 php --ini code>和 php -r'php_info();' code>和其他变种,以及 ini_get_all() code> function ,它可以显示php ini中设置的值 文件和任何被覆盖的值(例如来自 .htaccess code>或 ini_set code>)。 p>

php.net文档描述了一个默认设置 对于每个ini指令。 有没有办法从PHP代码中访问这些默认值? 这样我就可以对 ini_get_all code>的返回值进行一些简单的数组操作,并选出哪些具有非默认值。 p>

我在看 ini_restore code> 并且给出的示例读取就好像它一样 仅恢复到启动值,即ini文件中配置的值,而不是php默认值。 p> div>

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

This worked a charm! Before starting, I had a linked conf.d and I had overridden the cli/php.ini file to point to the apache2/php.ini file so that my php cli invocations would use the web server configs. The cli/php.ini file had been renamed .old, like so:

$ 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

I took the web server out of our load balancer pool and made some modifications.

$ 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)

I then added a file called check_config.php with these contents:

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

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

And got some delicious output.

$ /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
)

This did what I wanted, but it has a lot of red herrings - a lot of directives have a default value but are not included in the ini file and similarly a lot of module specific directives that are not in the values returned by ini_get_all(). I suppose I could improve this a bit by further finagling the config setups to enable more modules which should get their directives included in the list, but there are rather a lot of modules, so I think I'm good for now.