未在include语句内执行的PHP文件


PHP files not executing inside include statements

我有一个非常简单的网站:

index.html包含:

<html>
<body>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>

它在/usr/share/nginx/html 中

footer.php在同一目录中,并且是:

<?php
echo "<p>printed!!</p>";
?>

当我去http://MY_IP_address/footer.php我只得到了一张空白页的正确输出,左上角有"打印!!"。

当我去http://MY_IP_address/我得到:"欢迎来到我的主页!

一些文字。

还有一些文字。"

但我在任何地方都不会被写上"印刷品!!"。

我假设我在配置文件中遗漏了一些内容,但我不知道它是什么。

包含include()的文件是一个扩展名为.html的文件,因此Apache不会对其进行解释。因此,index.html中的代码不包括在内。(改进的解释归功于@RiggsFilly)

除非您的服务器设置为将.html文件解析为PHP,否则它将不会包含在内。

因此,您需要将其重命名为index.php

PHP中的include()函数包含其他PHP文件中的代码,并将它们复制到使用include语句的文件中。

来源:

  • https://stackoverflow.com/tags/php-include/info
  • http://php.net/manual/en/function.include.php

Php标记(<?php ?>)只能在文件扩展名为Php时解析。所以将文件名从index.html更改为index.php