【问题标题】:Creating a PHP header/footer创建 PHP 页眉/页脚
【发布时间】:2011-12-24 16:11:02
【问题描述】:

我正在为一个朋友设计一个相对简单的网站。我想实现 php,这样他就可以更改他的页眉/页脚,而不必返回每个文件。问题是,我对 php 的工作原理几乎完全不熟悉。有没有一种简单的方法可以做到这一点?我已经看到了一些关于如何制作 php 标头的答案,但它们看起来都不同,而且我没有取得太大的成功。我并不是说它们不起作用(我可能做错了),但在这种情况下越简单越好。

谢谢!

【问题讨论】:

  • 下面的答案应该对您有所帮助,但是当将来出现更大范围的麻烦时,像 smarty 或 twig 这样的模板引擎可以为您节省很多麻烦......它相对简单包含大量 HTML 和一些变量的学习曲线...

标签: php header footer


【解决方案1】:

除了使用 include()include_once() 来包含页眉和页脚之外,我发现有用的一件事是能够为每个页面包含自定义页面标题或自定义头部标签,但仍然有部分包含中的标头。我通常这样做如下:

在网站页面中:

<?php

$PageTitle="New Page Title";

function customPageHeader(){?>
  <!--Arbitrary HTML Tags-->
<?php }

include_once('header.php');

//body contents go here

include_once('footer.php');
?>

并且,在 header.php 文件中:

<!doctype html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title><?= isset($PageTitle) ? $PageTitle : "Default Title"?></title>
    <!-- Additional tags here -->
    <?php if (function_exists('customPageHeader')){
      customPageHeader();
    }?>
  </head>
  <body>

可能有点超出您原始问题的范围,但允许使用包含更多的灵活性很有用。

【讨论】:

  • 我认为应该是 <php> 而不是 <b><title>=
  • @Adam 在 PHP 中启用 Short Tags 的情况下,&lt;?= 等效于 &lt;php echo
  • 谢谢!很高兴知道!
  • 我无法再编辑我的评论,但我注意到一个错字 - 它应该是 &lt;?php echo
  • 为什么customPageHeader函数被php标签减半?您放在 部分中的任何内容都不会作为 php 运行,那么为什么它会在该 php 函数中?
【解决方案2】:

只需创建 header.php 文件,然后在你想使用它的地方做:

<?php
include('header.php');
?>

与页脚相同。如果您只有 html,则这些文件中不需要 php 标签。

在此处查看有关包含的更多信息:

http://php.net/manual/en/function.include.php

【讨论】:

    【解决方案3】:

    您可以通过在 php 中使用 include_once() 函数来实现。以header.php 的名称构造页眉部分,并以footer.php 构造页脚部分。最后将所有内容包含在一个文件中。

    例如:

    header.php

    <html>
    <title>
    <link href="sample.css">
    

    页脚.php

    </html>
    

    所以最终的文件看起来像

    include_once("header.php") 
    
    body content(The body content changes based on the file dynamically)
    
    include_once("footer.php") 
    

    【讨论】:

      【解决方案4】:

      您可以将其用于标题: 重要提示:将以下内容放在您想要包含内容的 PHP 页面上。

      <?php
      //at top:
      require('header.php'); 
       ?>
       <?php
      // at bottom:
      require('footer.php');
      ?>
      

      您也可以包含一个全局导航栏,只需使用它即可:

       <?php
       // At top:
      require('header.php'); 
       ?>
        <?php
      // At bottom:
      require('footer.php');
       ?>
       <?php
       //Wherever navbar goes:
      require('navbar.php'); 
      ?>
      

      在 header.php 中:

       <!DOCTYPE html>
       <html lang="en">
       <head>
          <meta charset="utf-8">
       <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
       </head>
       <body> 
      

      不要关闭 Body 或 Html 标签!
      在此处包含 html:

       <?php
       //Or more global php here:
      
       ?>
      

      页脚.php:

      代码在这里:

      <?php
      //code
      
      ?>
      

      Navbar.php:

      <p> Include html code here</p>
      <?php
       //Include Navbar PHP code here
       
      ?>
      

      好处:

      • 清理主 php 文件 (index.php) 脚本。
      • 更改页眉或页脚。等在所有页面上更改它包含 - 适合所有页面上的警报等...
      • 节省时间!
      • 更快的页面加载!
      • 您可以根据需要包含任意数量的文件!
      • 服务器端!

      【讨论】:

        【解决方案5】:

        越简单越好。

        index.php

        <? 
        if (empty($_SERVER['QUERY_STRING'])) { 
          $name="index"; 
        } else { 
          $name=basename($_SERVER['QUERY_STRING']); 
        } 
        $file="txt/".$name.".htm"; 
        if (is_readable($file)) { 
          include 'header.php';
          readfile($file);
        } else { 
          header("HTTP/1.0 404 Not Found");
          exit;
        } 
        ?>
        

        header.php

        <a href="index.php">Main page</a><br>
        <a href=?about>About</a><br>
        <a href=?links>Links</a><br>
        <br><br> 
        

        page.htm 格式存储在txt 文件夹中的实际静态html 页面

        【讨论】:

        • 这是一个有趣的方法!您会将其与 htaccess 重写一起使用,还是仅使用更简单的 www.somesite.com/?about txt 文件夹内是否仍有目录结构,或者基本名称是否消除了它?
        • 是的,我更喜欢将类似的方法与 mod_rewrite 结合使用,我什至有一些我相信的代码。如果没有 mod_rewrite,您必须坚持使用 ?page 方式。
        猜你喜欢
        • 2011-08-25
        • 2011-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-08
        相关资源
        最近更新 更多