【问题标题】:Is it possible to add html to phpinfo page?是否可以将 html 添加到 phpinfo 页面?
【发布时间】:2018-06-02 18:13:30
【问题描述】:

所以我正在创建自己的 phpinfo.php 页面。它回显了很好的 phpinfo(),但我想向它添加 html。我想在页面顶部添加一个后退按钮,让我回到我的主目录。然后在那个链接下面我将显示 phpinfo()。我注意到 phpinfo() 生成了一个包含 doctype 和所有内容的完整 html 文档。有解决方法吗?因为如果我在调用 phpinfo() 之前添加自己的 html 标签,最终结果将是一些 html 标签下的 html 文档(这显然不是 w3 标准)。我想从 phpinfo 页面获取 html 输出并将其复制到文件中,这样我就不会调用 phpinfo() 但如果我将来更改某些内容,我必须记住获取新信息,复制,再次将html粘贴到文档中。

那么有没有办法解决这个问题?

【问题讨论】:

  • 你可以使用 iFrame
  • 我完全忘记了 iFrame(主要是因为我之前从未在实际项目中使用过 iFrame)。感谢您的快速回复
  • 或者你可以使用 Ajax。
  • @Vince 没问题!
  • 如果你想创建自己的 phpinfo,你可以使用 php 函数,例如:apache_get_modules()(如果你使用 apache),ini_get_all(),php_ini_scanned_files (@see php.net/manual/en/function.php-ini-scanned-files.php)...

标签: php html phpinfo


【解决方案1】:

注意:在大多数情况下,您应该使用Genjo's answer。如果您没有 PHP DOM 模块(由 Debian 中的 php-xml 包提供),并且无论出于何种原因,您不想安装它,这里的这个答案提供了一个替代方案。

您可以像这样将 HTML 添加到 phpinfo 页面:

  • 获取原始 phpinfo 的 HTML。
  • 将 phpinfo 的 HTML 放入 iframe。
  • 使用 JavaScript 操作 iframe 内的 DOM 以添加/删除 HTML。

<h1>Hello!</h1> 添加到页面顶部的示例代码:

<?php
ob_start();
phpinfo();
$phpInfoHtml = htmlentities(ob_get_clean());
?>
<!DOCTYPE html>
<head>
    <title>phpinfo</title>
    <meta charset="utf-8">
</head>
<body>
    <style>
        #iframe {
            /* Make the iframe fill the whole page. */
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            border: none;
        }
    </style>

    <script>
        // Add HTML and CSS into the iframe.
        function editFrame() {
            var iframe = document.getElementById('iframe').contentDocument;
            var body = iframe.getElementsByTagName('body')[0];

            // Add a header.
            var div = iframe.createElement('div');
            div.innerHTML =
                '<div id="header">' +
                    '<h1>Hello!</h1>' +
                '</div>';
            body.insertBefore(div, body.firstChild);

            // Add CSS for the header.
            var css =  // Note: Only one CSS rule allowed per call of insertRule.
                '#header {' +
                    'width: 930px;' +
                    'margin: auto;' +
                '}';
            var sheet = iframe.styleSheets[0];
            sheet.insertRule(css, sheet.cssRules.length);
        }
    </script>

    <!-- The iframe. -->
    <iframe id="iframe" srcdoc="<?php echo $phpInfoHtml ?>" onload="editFrame();" />
</body>

结果:

【讨论】:

    【解决方案2】:

    你可以捕获 phpinfo() 的输出:

    // Get phpinfo() HTML
    ob_start();
    phpinfo();
    $phpInfoHtml = ob_get_clean();
    

    然后使用 DOM 和 XPath 提取您想要的部分 HTML:

    // Load HTML from string
    try {
        $doc = (new DOMDocument())->loadHTML($phpInfoHtml);
    } catch (Exception $e) {
        die('Caught exception: ',  $e->getMessage(), "\n");
    }
    
    // XPATH
    $xpath = new DOMXpath($doc);
    // Scrape your desired node(s)
    $body = $xpath->query('//body');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多