PHP 包含页眉菜单页脚适用于 index.php,但不适用于其他页面

发布于 2025-01-09 22:14:00 字数 1222 浏览 0 评论 0原文

我尝试用我的 html 代码替换包含 PHP 引用。它适用于index.php,但三个包含、页眉菜单和页脚不会出现在其他页面上,尽管内容和空格都在那里。

我使用原来的工作index.html 制作了一个名为index.php 的副本并保存了它。 我每次都会删除页眉、菜单和页脚 html 代码来替换包含指令。 我将这些 html 代码片段保存为 3 个 html 文件。其余文件内容保存为模板。 在网络托管网站上试用一切都很好。页眉菜单和页脚均显示正确。 然后我使用模板制作其他页面,仅输入从index.php开始正确显示的文本和图形,然后使用浏览器后退按钮检查其他页面。 我检查了原始的 index.php 和模板,以确保我只删除了 3 个文件的内容。 所有文件都位于主机上的一个文件夹中。 我缺少一些基本的东西,可能是要包含的三个文件没有给出绝对路径吗?

网站是 www.crowegenealogy.net 供参考?

模板代码如下

<!DOCTYPE html>
<html lang="en">
  <head>
     <title>Crowe Genealogy - Ireland and the World</title>
     <meta http-equiv="content-type" content="text/html; charset=utf-8">
     <link href="css/layout.css" rel="stylesheet" type="text/css">
     <link href="css/menu.css" rel="stylesheet" type="text/css">
  </head>
 <body>
  <?php include 'header.html' ;?> <br>
  <div class="container"><br>
  <ul id="nav">
  <?php include 'menu.html' ;?>
     </ul>
     </div>
     <p><br>
    <?php include 'footer.html' ;?> </p>
  </body>
</html>

I have tried substituting my html code for include PHP references. It works on the index.php but the three includes, header menu and footer do not appear on the other pages, though the content and spaces are there.

I used the original working index.html to make a copy called index.php and saved this.
I chop out the header, menu and footer html code substituting an include instruction each time.
I saved these snippets html code as 3 html files. The remaining file contents were saved as a template.
Everything is good on the trial on the web hosting site. Header Menu and Footer all display correctly.
I used the template then to make other pages, inputting only the text and graphics which all display correctly from the index.php start and I use the browser back button to check other pages.
I have checked the original index.php alongside the template to make sure I only removed the content of the 3 files.
All the files are in one folder on the host.
I am missing something basic, could it be the three files to be included are not given absolute paths?

website is www.crowegenealogy.net for reference?

template code below

<!DOCTYPE html>
<html lang="en">
  <head>
     <title>Crowe Genealogy - Ireland and the World</title>
     <meta http-equiv="content-type" content="text/html; charset=utf-8">
     <link href="css/layout.css" rel="stylesheet" type="text/css">
     <link href="css/menu.css" rel="stylesheet" type="text/css">
  </head>
 <body>
  <?php include 'header.html' ;?> <br>
  <div class="container"><br>
  <ul id="nav">
  <?php include 'menu.html' ;?>
     </ul>
     </div>
     <p><br>
    <?php include 'footer.html' ;?> </p>
  </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

粉红×色少女 2025-01-16 22:14:00

SJacks 提供了答案,我发现它在其他地方重复出现,尽管它并不总是有效。它只是证明很多教程都是学术解决方案或部分解决方案,而不是现实世界。 PHP 在某些页面上被吹捧为旧的 html 网站提供了一个简单的答案,但这条简单的道路充满了更多的构建和实现技术问题,需要对 PHP 整体有更深入的了解。最好回到首要原则并创建新网站,对可以恢复的内容进行限制,以避免冗长、乏味的挫败感 - 当您获得报酬时很重要!? :)

SJacks provided the answer and I found it repeated elsewhere, though it doesn't always work. It just proved that a lot of tutorials are academic solutions or part solutions and not real world. PHP is touted on some pages as providing a simple answer to old html sites but the simple road is fraught with more technical problems of construction and implementation, needing a greater understanding of PHP as a whole. Better to go back to first principles and make the new site, canabalising what can be recovered to avoid lengthy, tedious frustration - important when you are being paid!? :)

沙沙粒小 2025-01-16 22:14:00

您的其他页面无法工作,因为它们是由 HTML 文件组成的。我点击了分析> Crowe Locations,它带我到http://www.crowegenealogy.net/analysis_1.html (这是一个 HTML 文件)。正如你所说,标题不起作用。因此,我提取了源代码,发现您试图在其中使用 PHP 代码,但这不起作用。您的 PHP 代码没有被执行,因为它位于 .php 文件中(并且您当前没有使用 .htaccess 来允许在 html 文件中运行 php 代码(请参阅下面的 SJacks 评论)。其他回答者和评论者是对的,您应该对所有这些文件使用 .php 扩展名。可以使用仅包含 HTML 的 .php 文件。

Your other pages don't work because they're made from HTML files. I clicked on Analysis > Crowe Locations and it took me to http://www.crowegenealogy.net/analysis_1.html (which is an HTML file). The header wasn't working, as you said. So, I pulled up the source code and found that you were trying to use PHP code in there, and that's what's not working. your PHP code isn't being executed because it's in a .php file (and you are not currently employing .htaccess to allow running php code in an html file (see SJacks' comment below). The other answerers and commenters are right that you should be using the .php extension for all these files. It's ok to have a .php file that has only HTML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文