I'd like to compare the settings I have on 2 different servers. Both are shared hosting so I don't think I have enough access to do it any other way but programmatically with phpinfo. So now that I have the 2 outputs, I'd like to compare them without examining them manually. Is there an automated way for this?
Also, as a side but related note, I think phpinfo is the output of php.ini. Is this correct?
From the PHP Manual on phpinfo()
:
Outputs a large amount of information about the current state of PHP. This includes information about PHP compilation options and extensions, the PHP version, server information and environment (if compiled as a module), the PHP environment, OS version information, paths, master and local values of configuration options, HTTP headers, and the PHP License.
phpinfo()
does more than just printing out php.ini
settings.
If you want to process php.ini
settings manually, you might want to check out ini_get_all()
instead of phpinfo()
. This returns an array of all configuration values.
You could transfer the output of ini_get_all()
from server A to server B (for example by using var_export()
to create PHP code to create the array, or serialize()
), then use array_diff_assoc()
to compare the settings.
export.php: (Server A)
<?php echo serialize(ini_get_all()); ?>
compare.php: (Server B)
<?php
function ini_flatten($config) {
$flat = array();
foreach ($config as $key => $info) {
$flat[$key] = $info['local_value'];
}
return $flat;
}
function ini_diff($config1, $config2) {
return array_diff_assoc(ini_flatten($config1), ini_flatten($config2));
}
$config1 = ini_get_all();
$export_script = 'http://server-a.example.com/export.php';
$config2 = unserialize(file_get_contents($export_script));
$diff = ini_diff($config1, $config2);
?>
<pre><?php print_r($diff) ?></pre>
Found this question by searching and subsequently found a script which does exactly what I wanted (and I expect the OP wanted too).
https://github.com/brettalton/phpinfo-compare
Assuming you have a web server with curl, just put compare.php in a directory and changes the values of $site1 and $site2 to urls that fetch phpinfo and you got yourself a easy to read, pretty print diff. See example on his blog - http://blog.brettalton.com/2012/08/09/phpinfo-compare/
Comparing two php.ini files easily using a parse_ini_file function
Example code snippet
$firstIni = parse_ini_file('/etc/php5/apache2/php.ini');
$secondIni = parse_ini_file('/etc/php5/apache2/php.ini.save');
$firstIniDiff = array_diff($firstIni, $secondIni);
$secondIniDiff = array_diff($secondIni, $firstIni);
if (count($firstIniDiff) > 0) {
echo '<h1>php.ini changes</h1>';
echo '<ol>';
foreach ($firstIniDiff as $key => $val) {
echo '<li> php.ini'.$key.': '.$val.' ----> php.ini.save :'.@$secondIniDiff[$key].'</li> ';
}
echo '</ol>';
}
if (count($secondIniDiff) > 0) {
echo '<h1>php.ini.save changes</h1>';
echo '<ol>';
foreach ($secondIniDiff as $key => $val) {
echo '<li> php.ini'.$key.': '.$val.' ----> php.ini.save :'.@$firstIniDiff[$key].'</li> ';
}
echo '</ol>';
}
Output
If you just want to compare two php.ini files:
#!/usr/bin/php
<?php
function load($export_script) {
$f1 = file($export_script);
foreach($f1 as $line ) {
if( $line[0] == ';' ) continue;
if( $line[0] == '[' ) continue;
if( trim($line) == "" ) continue;
$var = explode( '=', $line);
$config1[trim($var[0])] = trim($var[1]);
}
return $config1;
}
$export_script = 'php.ini';
$c1 = load($export_script);
$export_script = 'php52.ini';
$c2 = load($export_script);
foreach( $c1 as $key => $val ) {
if( $val == $c2[$key] ) continue;
echo $key.': '.$c1[$key].' // '.$c2[$key]."\n";
}
?>
<?php var_dump(ini_get_all()); ?>
Then take the outputs and paste them into https://www.diffnow.com or your favorite diff-er.