简体   繁体   English

PHP文件未在包含语句中执行

[英]PHP files not executing inside include statements

I have an extraordinarily simple website: 我有一个非常简单的网站:

index.html contains: 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>

It is in /usr/share/nginx/html 它在/ usr / share / nginx / html中

footer.php is in the same directory and is: footer.php在同一目录中,并且是:

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

When I go to http://MY_IP_address/footer.php I get the proper output of just a blank page with "printed!!" 当我转到http://MY_IP_address/footer.php时,我得到的只是带有“打印!”的空白页的正确输出。 at the top left. 在左上方。

When I go to http://MY_IP_address/ I get: " Welcome to my home page! 当我转到http:// MY_IP_address /时,我得到:“欢迎来到我的主页!

Some text. 一些文字。

Some more text. 一些更多的文字。 "

But I do not get "printed!!" 但是我没有得到“印刷!” written anywhere. 写在任何地方。

I am assuming that somewhere I missed something in my config files, but I can't figure out what it is. 我假设我在某个地方错过了配置文件中的某些内容,但是我不知道是什么。

The file which contained include() is a file with .html extension, thus it's not interpreted by Apache. 包含include()的文件是扩展名为.html的文件,因此Apache不会对其进行解释。 Thus, the code in index.html was not included. 因此,不包括index.html中的代码。 (Credits to @RiggsFolly for the improved explanation) (感谢@RiggsFolly提供改进的解释)

Unless your server is set to parse .html files as PHP, it will not be included. 除非您的服务器设置为将.html文件解析为PHP,否则它将不包括在内。

Thus you'll need to rename it to index.php . 因此,您需要将其重命名为index.php

The include() function in PHP includes code from other PHP files and copies them into the file that uses the include statement. PHP中的include()函数包含来自其他PHP文件的代码,并将其复制到使用include语句的文件中。

Source: 资源:

Php tags( <?php ?> ) can only be parsed when file extension is php. 仅当文件扩展名是php时才能解析php标签( <?php ?> )。 So, Change file name from index.html to index.php 因此,将文件名从index.html更改为index.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM