To extract all of the data from phpinfo into a nested array:
<?php
ob_start();
phpinfo();
$phpinfo = array('phpinfo' => array());
if(preg_match_all('#(?:<h2>(?:<a name=".*?">)?(.*?)(?:</a>)?</h2>)|(?:<tr(?: class=".*?")?><t[hd](?: class=".*?")?>(.*?)\s*</t[hd]>(?:<t[hd](?: class=".*?")?>(.*?)\s*</t[hd]>(?:<t[hd](?: class=".*?")?>(.*?)\s*</t[hd]>)?)?</tr>)#s', ob_get_clean(), $matches, PREG_SET_ORDER))
foreach($matches as $match)
if(strlen($match[1]))
$phpinfo[$match[1]] = array();
elseif(isset($match[3]))
$phpinfo[end(array_keys($phpinfo))][$match[2]] = isset($match[4]) ? array($match[3], $match[4]) : $match[3];
else
$phpinfo[end(array_keys($phpinfo))][] = $match[2];
?>
Some examples of using individual values from the array:
<?php
echo "System: {$phpinfo['phpinfo']['System']}<br />\n";
echo "Safe Mode: {$phpinfo['PHP Core']['safe_mode'][0]}<br />\n";
echo "License: {$phpinfo['PHP License'][0]}<br />\n";
?>
To display everything:
<?php
foreach($phpinfo as $name => $section) {
echo "<h3>$name</h3>\n<table>\n";
foreach($section as $key => $val) {
if(is_array($val))
echo "<tr><td>$key</td><td>$val[0]</td><td>$val[1]</td></tr>\n";
elseif(is_string($key))
echo "<tr><td>$key</td><td>$val</td></tr>\n";
else
echo "<tr><td>$val</td></tr>\n";
}
echo "</table>\n";
}
?>
Note: In order to properly retrieve all of the data, the regular expression matches table headers as well as table data, resulting in 'Local Value' and 'Global Value' showing up as 'Directive' entries.
phpinfo
(PHP 4, PHP 5)
phpinfo — Affiche de nombreuses informations sur PHP
Description
Affiche de nombreuses informations sur PHP, concernant sa configuration courante : options de compilation, extensions, version, informations sur le serveur, et l'environnement (lorsqu'il est compilé comme module), environnement PHP, informations sur le système, chemins, valeurs générales et locales de configuration, en-têtes HTTP et la licence PHP.
Comme tous les systèmes sont configurés différemment, phpinfo() sert généralement à vérifier la configuration ainsi que les variables prédéfinies, pour une plate-forme donnée.
phpinfo() est un bon outil de débogage, car il affiche le contenu de toutes les variables EGPCS (Environnement, GET, POST, Cookie, Serveur).
Liste de paramètres
- what
-
L'affichage peut être personnalisé en utilisant une ou plusieurs des constantes suivantes. Elles sont combinables avec l'opérateur or, et doivent être passées dans le paramètre what . Vous pouvez aussi additionner ces constantes.
Options de phpinfo() Nom de la constante Valeur Description INFO_GENERAL 1 La ligne de configuration, le chemin du php.ini, la date de compilation, le serveur web, le système, etc. INFO_CREDITS 2 Les crédits de PHP. Voir aussi phpcredits(). INFO_CONFIGURATION 4 Valeurs courantes locales et générales des directives PHP. Voyez aussi la fonction ini_get(). INFO_MODULES 8 Modules chargés et leur configuration spécifique. Voir aussi la fonction get_loaded_extensions(). INFO_ENVIRONMENT 16 Informations sur les variables d'environnement, qui sont disponibles dans la variable $_ENV. INFO_VARIABLES 32 Affiche toutes les variables prédéfinies, issues de l'environnement, la méthode GET, la méthode POST, les cookies et le serveur. INFO_LICENSE 64 La licence PHP. Voir aussi » la FAQ de la licence. INFO_ALL -1 Affiche toutes les informations suscitées. C'est la valeur par défaut.
Valeurs de retour
Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec.
Historique
| Version | Description |
|---|---|
| 5.2.2 | L'information "Loaded Configuration File" a été ajoutée, alors qu'avant, seule l'information "Configuration File (php.ini) Path" existée. |
Exemples
Exemple #1 Exemple avec phpinfo()
<?php
// Affiche toutes les informations, comme le ferait INFO_ALL
phpinfo();
// Affiche uniquement le module d'information.
// phpinfo(8) fournirait les mêmes informations.
phpinfo(INFO_MODULES);
?>
Notes
Note: Une partie des informations affichées sont désactivées si la directive expose_php est configurée avec la valeur off. Cela inclus les logos PHP et Zend, ainsi que les crédits.
Note: phpinfo() affiche du texte au lieu de HTML lorsque vous utilisez la version CLI.
phpinfo
06-Jul-2008 04:54
23-Jun-2008 04:24
big thanx 2 Mardy dot Hutchinson at gmail dot com
very good!
some fixes to correct result displaying:
1. we need to trim $matches [1], 'cause there can be empty lines;
2. not bad to remove <body> tag 'cause styles for it not apply correctly...
3. ...and change styles a little (remove "body" selector)
we need to change two lines:
<?php
preg_match ('%<style type="text/css">(.*?)</style>.*?(<body>.*</body>)%s', ob_get_clean(), $matches);
?>
to
<?php
preg_match ('%<style type="text/css">(.*?)</style>.*?<body>(.*?)</body>%s', ob_get_clean(), $matches);
?>
and
<?php
preg_split( '/\n/', $matches[1] )
?>
to
<?php
preg_split( '/\n/', trim(preg_replace( "/\nbody/", "\n", $matches[1])) )
?>
That's all! Now we have a really flexible addition to phpinfo();
10-Sep-2007 12:27
Embedding phpinfo within your page, that already has style information:
The phpinfo output is wrapped within a <div class='phpinfodisplay'>, and we privatize all the style selectors that phpinfo() creates.
Yes, we cheat on preparing the selector list.
<?php
ob_start();
phpinfo();
preg_match ('%<style type="text/css">(.*?)</style>.*?(<body>.*</body>)%s', ob_get_clean(), $matches);
# $matches [1]; # Style information
# $matches [2]; # Body information
echo "<div class='phpinfodisplay'><style type='text/css'>\n",
join( "\n",
array_map(
create_function(
'$i',
'return ".phpinfodisplay " . preg_replace( "/,/", ",.phpinfodisplay ", $i );'
),
preg_split( '/\n/', $matches[1] )
)
),
"</style>\n",
$matches[2],
"\n</div>\n";
?>
Perhaps one day the phpinfo() function will be modified to output such a safe string on its own.
09-Sep-2007 09:47
One note on the above functions for cleaning up the phpinfo() HTML and throwing it into an array data structure. In order to catch all of the info tidbits the preg_match_all has to be tweaked to deal with 2 and 3 column tables.
I have changed the preg_match_all() here so that the last <td></td> is optional
<?php
function parsePHPConfig() {
ob_start();
phpinfo(-1);
$s = ob_get_contents();
ob_end_clean();
$a = $mtc = array();
if (preg_match_all('/<tr><td class="e">(.*?)<\/td><td class="v">(.*?)<\/td>(:?<td class="v">(.*?)<\/td>)?<\/tr>/',$s,$mtc,PREG_SET_ORDER))
foreach($mtc as $v){
if($v[2] == '<i>no value</i>') continue;
$a[$v[1]] = $v[2];
}
}
return $a;
}
?>
14-Aug-2007 11:43
Here's a variant of "print it without headers, but include the style information":
<?php
ob_start();
phpinfo();
$info = ob_get_clean ();
$matches = array ();
$i = preg_match ('%(<style type="text/css">.*</style>).*<body>(.*)</body>%s', $info, $matches);
print $matches [1]; # Style information
print $matches [2]; # Body
07-Jan-2007 10:35
same as above for configuration variables
<?php
function parsePHPConfig() {
ob_start();
phpinfo(INFO_CONFIGURATION);
$s = ob_get_contents();
ob_end_clean();
$a = $mtc = array();
if (preg_match_all('/<tr><td class="e">(.*?)<\/td><td class="v">(.*?)<\/td><td class="v">(.*?)<\/td><\/tr>/',$s,$mtc,PREG_SET_ORDER)) {
foreach($mtc as $v){
if($v[2] == '<i>no value</i>') continue;
$a[$v[1]] = $v[2];
}
}
return $a;
}
?>
11-Oct-2006 06:29
This is a slight modification to the previous code by "code at adspeed dot com" that extracts the PHP modules as an array. I used it on PHP 4.1.2 and it failed as the <h2> tags also had an align="center". So this update changes the regex for those tags:
<?php
/* parse php modules from phpinfo */
function parsePHPModules() {
ob_start();
phpinfo(INFO_MODULES);
$s = ob_get_contents();
ob_end_clean();
$s = strip_tags($s,'<h2><th><td>');
$s = preg_replace('/<th[^>]*>([^<]+)<\/th>/',"<info>\\1</info>",$s);
$s = preg_replace('/<td[^>]*>([^<]+)<\/td>/',"<info>\\1</info>",$s);
$vTmp = preg_split('/(<h2[^>]*>[^<]+<\/h2>)/',$s,-1,PREG_SPLIT_DELIM_CAPTURE);
$vModules = array();
for ($i=1;$i<count($vTmp);$i++) {
if (preg_match('/<h2[^>]*>([^<]+)<\/h2>/',$vTmp[$i],$vMat)) {
$vName = trim($vMat[1]);
$vTmp2 = explode("\n",$vTmp[$i+1]);
foreach ($vTmp2 AS $vOne) {
$vPat = '<info>([^<]+)<\/info>';
$vPat3 = "/$vPat\s*$vPat\s*$vPat/";
$vPat2 = "/$vPat\s*$vPat/";
if (preg_match($vPat3,$vOne,$vMat)) { // 3cols
$vModules[$vName][trim($vMat[1])] = array(trim($vMat[2]),trim($vMat[3]));
} elseif (preg_match($vPat2,$vOne,$vMat)) { // 2cols
$vModules[$vName][trim($vMat[1])] = trim($vMat[2]);
}
}
}
}
return $vModules;
}
?>
10-Sep-2006 12:32
To obtain a phpinfo without headers (and css) :
<?php
ob_start();
phpinfo();
$info = ob_get_contents();
ob_end_clean();
$info = preg_replace('%^.*<body>(.*)</body>.*$%ms', '$1', $info);
?>
You can then style your tables & headings :)
10-Dec-2005 12:31
This function parses the phpinfo output to get details about a PHP module.
<?php
/** parse php modules from phpinfo */
function parsePHPModules() {
ob_start();
phpinfo(INFO_MODULES);
$s = ob_get_contents();
ob_end_clean();
$s = strip_tags($s,'<h2><th><td>');
$s = preg_replace('/<th[^>]*>([^<]+)<\/th>/',"<info>\\1</info>",$s);
$s = preg_replace('/<td[^>]*>([^<]+)<\/td>/',"<info>\\1</info>",$s);
$vTmp = preg_split('/(<h2>[^<]+<\/h2>)/',$s,-1,PREG_SPLIT_DELIM_CAPTURE);
$vModules = array();
for ($i=1;$i<count($vTmp);$i++) {
if (preg_match('/<h2>([^<]+)<\/h2>/',$vTmp[$i],$vMat)) {
$vName = trim($vMat[1]);
$vTmp2 = explode("\n",$vTmp[$i+1]);
foreach ($vTmp2 AS $vOne) {
$vPat = '<info>([^<]+)<\/info>';
$vPat3 = "/$vPat\s*$vPat\s*$vPat/";
$vPat2 = "/$vPat\s*$vPat/";
if (preg_match($vPat3,$vOne,$vMat)) { // 3cols
$vModules[$vName][trim($vMat[1])] = array(trim($vMat[2]),trim($vMat[3]));
} elseif (preg_match($vPat2,$vOne,$vMat)) { // 2cols
$vModules[$vName][trim($vMat[1])] = trim($vMat[2]);
}
}
}
}
return $vModules;
}
?>
Sample Output:
[gd] => Array
(
[GD Support] => enabled
[GD Version] => bundled (2.0.28 compatible)
[FreeType Support] => enabled
[FreeType Linkage] => with freetype
[FreeType Version] => 2.1.9
[T1Lib Support] => enabled
[GIF Read Support] => enabled
[GIF Create Support] => enabled
[JPG Support] => enabled
[PNG Support] => enabled
[WBMP Support] => enabled
[XBM Support] => enabled
)
[date] => Array (
[date/time support] => enabled
[Timezone Database Version] => 2005.14
[Timezone Database] => internal
[Default timezone] => America/Los_Angeles
[Directive] => Array (
[0] => Local Value
[1] => Master Value
)
[date.timezone] => Array (
[0] => no value
[1] => no value
)
)
<?php
/** get a module setting */
function getModuleSetting($pModuleName,$pSetting) {
$vModules = parsePHPModules();
return $vModules[$pModuleName][$pSetting];
}
?>
Example: getModuleSetting('gd','GD Version'); returns "bundled (2.0.28 compatible)"
06-Oct-2005 05:38
check out this cool and fantastic colourful phpinfo()!
<?php
ob_start();
phpinfo();
$phpinfo = ob_get_contents();
ob_end_clean();
preg_match_all('/#[0-9a-fA-F]{6}/', $phpinfo, $rawmatches);
for ($i = 0; $i < count($rawmatches[0]); $i++)
$matches[] = $rawmatches[0][$i];
$matches = array_unique($matches);
$hexvalue = '0123456789abcdef';
$j = 0;
foreach ($matches as $match)
{
$r = '#';
$searches[$j] = $match;
for ($i = 0; $i < 6; $i++)
$r .= substr($hexvalue, mt_rand(0, 15), 1);
$replacements[$j++] = $r;
unset($r);
}
for ($i = 0; $i < count($searches); $i++)
$phpinfo = str_replace($searches, $replacements, $phpinfo);
echo $phpinfo;
?>
