我在測試網站時,會查看 phpinfo 資訊,用以檢查問題在那裡
一般情況,會這樣子寫
<?php phpinfo();?>
但這如果要用於比對的話,會很辛苦
所以我只要幾個值就好 (如下)
<?php
print 'sendmail_from = ' . ini_get('sendmail_from') . "<br>\n";
print 'sendmail_path = ' . ini_get('sendmail_path') . "<br>\n";
print 'open_basedir = ' . ini_get('open_basedir') . "<br>\n";
print 'upload_tmp_dir = ' . ini_get('upload_tmp_dir') . "<br>\n";
print 'session.save_path = ' . ini_get('session.save_path') . "<br>\n";
print 'directory = ' .$_SERVER["DOCUMENT_ROOT"] . "<br>\n";
print 'safe_mode = ' . ini_get('safe_mode') . "<br>\n";
print 'disable_functions = ' . ini_get('disable_functions') . "<br>\n";
print 'allow_url_fopen = ' . ini_get('allow_url_fopen') . "<br>\n";
?>
顯示
sendmail_from = [email protected] sendmail_path = /usr/sbin/sendmail -t -i -f [email protected] open_basedir = /var/www/html/ssorc.tw:/tmp/ssorc.tw upload_tmp_dir = /tmp/ssorc.tw session.save_path = /tmp/ssorc.tw safe_mode = disable_functions = exec,passthru,proc_open,shell_exec,system,popen,dl allow_url_fopen = 0 directory = /var/www/html/ssorc.tw
phpinfo 裡會有 local 與 master 兩種,local 就是有自已定義在網站裡的,可能是 php_admin_flag XXX 等等,而 master 就是 /etc/php.ini 裡所定義的,但以上方式就只能得到 local 欄位值
我想要有 master 欄位的值,就要搭配 ob_start(),才能把 phpinfo 輸出到變數裡
我在 PHP 官網手冊找到有人寫好的 範例
<?php
function phpinfo_array($return=false){
/* Andale! Andale! Yee-Hah! */
ob_start();
phpinfo(-1);
$pi = preg_replace(
array('#^.*<body>(.*)</body>.*$#ms', '#<h2>PHP License</h2>.*$#ms',
'#<h1>Configuration</h1>#', "#\r?\n#", "#</(h1|h2|h3|tr)>#", '# +<#',
"#[ \t]+#", '# #', '# +#', '# class=".*?"#', '%'%',
'#<tr>(?:.*?)" src="(?:.*?)=(.*?)" alt="PHP Logo" /></a>'
.'<h1>PHP Version (.*?)</h1>(?:\n+?)</td></tr>#',
'#<h1><a href="(?:.*?)\?=(.*?)">PHP Credits</a></h1>#',
'#<tr>(?:.*?)" src="(?:.*?)=(.*?)"(?:.*?)Zend Engine (.*?),(?:.*?)</tr>#',
"# +#", '#<tr>#', '#</tr>#'),
array('$1', '', '', '', '</$1>' . "\n", '<', ' ', ' ', ' ', '', ' ',
'<h2>PHP Configuration</h2>'."\n".'<tr><td>PHP Version</td><td>$2</td></tr>'.
"\n".'<tr><td>PHP Egg</td><td>$1</td></tr>',
'<tr><td>PHP Credits Egg</td><td>$1</td></tr>',
'<tr><td>Zend Engine</td><td>$2</td></tr>' . "\n" .
'<tr><td>Zend Egg</td><td>$1</td></tr>', ' ', '%S%', '%E%'),
ob_get_clean());
$sections = explode('<h2>', strip_tags($pi, '<h2><th><td>'));
unset($sections[0]);
$pi = array();
foreach($sections as $section){
$n = substr($section, 0, strpos($section, '</h2>'));
preg_match_all(
'#%S%(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?%E%#',
$section, $askapache, PREG_SET_ORDER);
foreach($askapache as $m)
$pi[$n][$m[1]]=(!isset($m[3])||$m[2]==$m[3])?$m[2]:array_slice($m,2);
}
#return ($return === false) ? echo "<pre>",print_r($pi),"</pre>" : $pi;
echo "<pre>",print_r($pi),"</pre>"; # 我改了這裡,秀出來比較好看
}
phpinfo_array(); # 我加了這個
?>
以上就可以秀出陣列了,我再從中挑出我要的
顯示
Array ( [apache2handler] => Array ( [Apache Version] => Apache [Apache API Version] => 20051115 [Server Administrator] => [email protected] [Hostname:Port] => ssorc.tw:80 [User/Group] => apache(48)/48 [Max Requests] => Per Child: 4000 - Keep Alive: off - Max Per Connection: 100 [Timeouts] => Connection: 60 - Keep-Alive: 15 (省略)
我修改了符合我要的 (如下)
<?php
function phpinfo_array($return=false)
{
ob_start();
phpinfo(-1);
$pi = preg_replace(
array('#^.*<body>(.*)</body>.*$#ms', '#<h2>PHP License</h2>.*$#ms',
'#<h1>Configuration</h1>#', "#\r?\n#", "#</(h1|h2|h3|tr)>#", '# +<#',
"#[ \t]+#", '# #', '# +#', '# class=".*?"#', '%'%',
'#<tr>(?:.*?)" src="(?:.*?)=(.*?)" alt="PHP Logo" /></a>'
.'<h1>PHP Version (.*?)</h1>(?:\n+?)</td></tr>#',
'#<h1><a href="(?:.*?)\?=(.*?)">PHP Credits</a></h1>#',
'#<tr>(?:.*?)" src="(?:.*?)=(.*?)"(?:.*?)Zend Engine (.*?),(?:.*?)</tr>#',
"# +#", '#<tr>#', '#</tr>#'),
array('$1', '', '', '', '</$1>' . "\n", '<', ' ', ' ', ' ', '', ' ',
'<h2>PHP Configuration</h2>'."\n".'<tr><td>PHP Version</td><td>$2</td></tr>'.
"\n".'<tr><td>PHP Egg</td><td>$1</td></tr>',
'<tr><td>PHP Credits Egg</td><td>$1</td></tr>',
'<tr><td>Zend Engine</td><td>$2</td></tr>' . "\n" .
'<tr><td>Zend Egg</td><td>$1</td></tr>', ' ', '%S%', '%E%'),
ob_get_clean());
$sections = explode('<h2>', strip_tags($pi, '<h2><th><td>'));
unset($sections[0]);
$pi = array();
foreach($sections as $section)
{
$n = substr($section, 0, strpos($section, '</h2>'));
preg_match_all('#%S%(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?%E%#',$section, $askapache, PREG_SET_ORDER);
foreach($askapache as $m) $pi[$n][$m[1]]=(!isset($m[3])||$m[2]==$m[3])?$m[2]:array_slice($m,2);
}
if ($_SERVER["QUERY_STRING"] == "v=1") {
echo "<pre>",print_r($pi),"</pre>";
}
return $pi;
}
echo <<<EOF
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>PHPINFO</TITLE>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" >
<style type="text/css">
#d1 { align:center; background-color:lightblue; color:#000000; font-size:12pt; }
#s1 { width:20%; display:inline-block;}
#s2 { width:30%; display:inline-block; color:blue}
#s3 { width:30%; display:inline-block; color:green}
</style>
</HEAD>
<body>
EOF;
foreach (phpinfo_array() as $key => $val) {
#echo "$key<br>";
if (preg_match("/^Core/",$key)) {
foreach ($val as $directive => $value) {
if (preg_match("/^(sendmail_from|sendmail_path|open_basedir|upload_tmp_dir|session.save_path|safe_mode|disable_functions|allow_url_fopen)$/", $directive)) {
if (is_array($value)) {
echo "<div id=\"d1\"><span id=\"s1\">$directive</span><span id=\"s2\">$value[0]</span><span id=\"s3\">$value[1]</span></div>\n";
} else {
echo "<div id=\"d1\"><span id=\"s1\">$directive</span><span id=\"s2\">$value</span><span id=\"s3\">$value</span></div>\n";
}
}
}
}
if (preg_match("/^session/",$key)) {
foreach ($val as $directive => $value) {
if (preg_match("/^(session.save_path)$/", $directive)) {
if (is_array($value)) {
echo "<div id=\"d1\"><span id=\"s1\">$directive</span><span id=\"s2\">$value[0]</span><span id=\"s3\">$value[1]</span></div>\n";
} else {
echo "<div id=\"d1\"><span id=\"s1\">$directive</span><span id=\"s2\">$value</span><span id=\"s3\">$value</span></div>\n";
}
}
}
}
if (preg_match("/^PHP Variables/",$key)) {
foreach ($val as $directive => $value) {
if (preg_match("/DOCUMENT_ROOT/", $directive)) {
echo "<div id=\"d1\"><span id=\"s1\">$directive</span><span id=\"s2\">$value</span><span id=\"s3\"></span></div>\n";
}
}
}
}
echo <<<EOF
</body>
</html>
EOF;
?>
顯示
參考
- PHP
- http://justcoding.iteye.com/blog/1991828
- http://yiyingloveart.blogspot.tw/2012/10/phpvardumparray.html
留言
滿實用的,推推
3q la