【问题标题】:Embedding data into HTML page from shell_exec从 shell_exec 将数据嵌入 HTML 页面
【发布时间】:2023-03-28 05:25:01
【问题描述】:

我对 Web 开发相当陌生,遇到了一个小问题。 我正在我管理的集群上创建一个 html 网站。我想在主页 (index.html) 上显示服务器正常运行时间。

我用以下代码创建了一个 php 脚本(称为 test.php):

<?php
$uptime = shell_exec("cut -d. -f1 /proc/uptime");
$days = floor($uptime/60/60/24);
$hours = $uptime/60/60%24;
$mins = $uptime/60%60;
$secs = $uptime%60;
echo "up $days days $hours hours $mins minutes and $secs seconds";
?>

仅查看 test.php 文件时,代码本身就可以正常工作,但我想拥有它,以便我可以在 index.html 页面上获得相同的信息。

我不知道如何将 php 文件嵌入到 html 文件中。

【问题讨论】:

  • 为什么不用index.php 而不是html
  • include 它。

标签: php html linux embed


【解决方案1】:

&lt;iframe&gt; 标签被认为是 bad 的做法,您也不应该在&lt;body&gt; 中添加&lt;meta&gt; 标签。除非有正当理由,否则应避免使用。

最简单的解决方案是使用 PHP 而不是 HTML 作为索引页面。

但是,如果您确实需要使用 HTML,您可以通过 Ajax 从您的 PHP 文件中获取正常运行时间并将其注入到您的 HTML 文件中。这是一个使用 jQuery 的示例:

$.ajax({
    url: "test.php",
    type: "get",
    success: function(data) {
        $(".uptime").html(data);
    }
});

同样,这不是理想的解决方案,您应该真正考虑在整个网站上使用 PHP。

【讨论】:

  • 我同意你的观点,自从我发布这个问题以来的几个月里,我学到了很多关于 Web 开发的知识。我无法将其更改为 .php 的原因是因为一堆其他网站链接到 .html 页面(重定向将是另一种解决方案?)。但是现在知道该怎么做,我会向任何遇到此问题的人推荐这个!
【解决方案2】:

将网站设为 .php 而不是 .html,不会有太大的不同。你也可以使用

<?php system("uptime"); ?>

作为一种变体。

【讨论】:

  • uptime 不同于 /proc/uptime
【解决方案3】:

我最终只是使用了一个 html iframe。所以在我的 index.html 中包含了这一行

<iframe src="test.php"></iframe>

这为我完成了工作 我还通过将此行添加到 test.php 文件中添加了刷新功能

<meta http-equiv="refresh" content="15">

【讨论】:

    【解决方案4】:

    一般情况下,文件由网络服务器处理。

    在带有 PHP 模块的 Apache Web 服务器上您可以尝试写入.htaccess(文件可以在站点目录中):

    AddHandler 应用程序/x-httpd-php .php .htm .html

    AddType application/x-httpd-php .php .htm .html


    对于带有 phpfpm 的 Nginx,您应该执行两个步骤:

    1. 首先将security.limit_extensions = .php .html设置为php-fpm.conf

    2. 其次,您应该添加到 Nginx 的配置中:

    location ~* \.(php|htm|html)$ {
        fastcgi_pass ...
        fastcgi_param ...
        include fastcgi_params;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-31
      • 2015-12-22
      • 1970-01-01
      • 2013-09-15
      • 2013-11-30
      • 1970-01-01
      • 2014-04-01
      • 2012-12-18
      • 2015-01-19
      相关资源
      最近更新 更多