php 如何在网页中正确包含头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16632812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:28:41  来源:igfitidea点击:

How to properly include a header file in a webpage

phphtml

提问by erdomester

So I have a header file:

所以我有一个头文件:

<html>
<head>
   <link href="/design_header.css" rel="stylesheet" type="text/css" />
</head>
<body>
   content
</body>
</html>

And I want to place this header in my file

我想把这个标题放在我的文件中

  <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
     <head>
     meta tags etc.
    </head>
    <body> 
      <div id="container_big">
       <?php include 'header_login.php'; ?>
        content
      </div>
    </body>
    </html>

The problem is now I have regular <html>, <head>, <body>tags inside the <body>tag of my webpage. In the html validator I receive errors like

问题是现在我的网页标签中有常规<html>, <head>, <body>标签<body>。在 html 验证器中,我收到类似的错误

Stray start tag html.
Stray end tag head.
An body start tag seen but an element of th
Stray start tag html.
Stray end tag head.
An body start tag seen but an element of th

e same type was already open.

相同类型已打开。

How to do it properly? Btw. I also place a footer to the bottom of the webpage the same way.

如何正确操作?顺便提一句。我也以同样的方式在网页底部放置了一个页脚。

回答by Menios

Your header file should only contain the HTML text you want for the header. As it will be inserted into another webpage, it should not be a full HTML document.

您的头文件应该只包含您想要的标题的 HTML 文本。因为它将被插入到另一个网页中,所以它不应该是一个完整的 HTML 文档。

Having only HTML for Header in Header

Header 中只有 Header 的 HTML

One option is to instead include in your header file only the HTML that is for the header (used by all pages that include it). However this has the downside that your not following the recommendation to have CSS loading before is rendered.

一种选择是在您的头文件中仅包含用于标题的 HTML(由包含它的所有页面使用)。但是,这有一个缺点,即您没有遵循在呈现之前加载 CSS 的建议。

See: https://stackoverflow.com/a/1642259/1688441

参见:https: //stackoverflow.com/a/1642259/1688441

Header File

头文件

 <link href="/design_header.css" rel="stylesheet" type="text/css" />
 content

Other files

其它文件

  <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
     <head>
     meta tags etc.
    </head>
    <body> 
      <div id="container_big">
       <?php include 'header_login.php'; ?>
        content
      </div>
    </body>
    </html>

Having only HTML for Header in HeaderFile and Separate HeadFile

HeaderFile 中的 Header 和单独的 HeadFile 中只有 HTML

You could have have a separate global_head_include.html(or txt, php, etc) file and put your CSS code there.

您可以有一个单独的global_head_include.html(或 txt、php 等)文件并将您的 CSS 代码放在那里。

Header File (for include)

头文件(用于包含)

 Header content

File with CSS includes (for include)

带有 CSS 的文件包含(用于包含)

  <link href="/design_header.css" rel="stylesheet" type="text/css" />
Whatever else is global...

Other files

其它文件

  <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
     <head>
     meta tags etc.
      <?php include 'global_head_include.php'; ?>
    </head>
    <body> 
      <div id="container_big">
       <?php include 'header_login.php'; ?>
        content
      </div>
    </body>
    </html>

回答by James Wright

This is how I set out my headers and footers in my current PHP site:

这是我在当前 PHP 站点中设置页眉和页脚的方式:

header.php:

头文件.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
        <title>James Wright - Young Software Developer</title>
        <link rel="stylesheet" href="style.css" />
        <meta name="description" content="The online home of a young web designer and software developer." />
        <link rel="icon" type="image/png" href="/img/ico.png" />
    </head>
    <body>
        <div id="holder">
            <div id="menu"></div>
            <div id="mainContent">

footer.php:

页脚.php:

            </div>
            <div id="mainReflect"></div>

            <p class="footer">&copy; James Wright <?php echo date("Y"); ?>. Minimum resolution: 1024x768            <a href="http://validator.w3.org/check?uri=referer" target="_blank"><img
  src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Transitional" height="31" width="88" style="vertical-align: middle;"/></a>        
                <a href='http://www.powermapper.com/products/sortsite/'><img src='http://www.powermapper.com/images/badge-v1/sortsite-badge-small-5.png' width="80" height="15" alt='Broken link checker and accessibility checker top 5% - SortSite' style='vertical-align: middle;'/></a>             
            </p>
        </div>
    </body>
</html>

Then whenever I create a new page, all I need to do is this:

然后每当我创建一个新页面时,我需要做的就是:

<?php include("header.php"); ?>
<p>Main body content!</p>
<?php include("footer.php"); ?>