如何在 PHP 中包含其他文件?

How do I include other files in PHP?

我刚开始在家里创建网站。

当然,我必须有这两个页面才能快速完成我的网站:- footer.php, header.php.

所以我创建了这些页面并放置了一些内容。还在 htdocs 文件夹中创建了一个索引页 index.php

然后我使用以下代码将 headerfooter 页面包含在 index.php 页面中。

<?php include 'header.php'; ?>
<?php include 'footer.php'; ?>

毫无疑问,他们工作得很好,没有任何问题。

然后我在 htdocs.

中创建了一个目录 account

现在我在 account 目录 (/account/login.php) 中有一个 login.php 页面。

我反复使用相同的代码在登录页面中包含 headerfooter。但是他们没有用!我没有看到任何事情发生。如果我在 htdocs 文件夹中创建 login.php 页面(而不是在 htdocs/account/ 中),那么它就可以工作了。

那么,当登录页面位于 account 目录中时,我该如何包含它们?

创建子目录和包含文件时,使用绝对文件路径总是更简单。

参考根目录的路径称为绝对https://www.website.com/modules/header.php),您甚至可以删除域而只拥有/modules/header。php。引用当前目录的路径称为 relative (../images/phone.png)。 ../表示URL指向当前文件夹的上一级目录。

请在此处查看与类似问题相关的答案:difference-between-relative-path-and-absolute-path-in-javascript

我认为是文件路径问题,你可以使用这个代码:

<?php include '../header.php'  ?>
<?php include '../footer.php'  ?>

加载一级目录的文件。