从shell_exec将数据嵌入到HTML页面中

时间:2013-05-21 19:47:42

标签: php html linux embed

我是网络开发的新手,我遇到了一个小问题。 我正在我管理的集群上创建一个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文件中。

4 个答案:

答案 0 :(得分:1)

制作网站.php而不是.html,它不会产生很大的影响。您也可以使用

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

作为变种。

答案 1 :(得分:1)

<iframe>代码被视为错误做法,您也不应在<meta>中添加<body>代码。除非你有正当理由,否则你应该避免使用它们。

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

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

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

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

答案 2 :(得分:0)

通常,文件由Web服务器处理。

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

  

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

OR

  

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


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

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

  2. 其次你应该添加到Nginx的配置:

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

答案 3 :(得分:0)

我最终做的只是使用html iframe。所以在我的index.html中加入了行

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

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

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