Creating a PHP header/footer

后端 未结 5 797
长情又很酷
长情又很酷 2020-11-29 02:47

I\'m designing a relatively simple site for a friend. I would like to impliment php so that he can change his header/footer without having to go back through every file. P

相关标签:
5条回答
  • 2020-11-29 02:57

    Just create the header.php file, and where you want to use it do:

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

    Same with the footer. You don't need php tags in these files if you just have html.

    See more about include here:

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

    0 讨论(0)
  • 2020-11-29 02:59

    You can do it by using include_once() function in php. Construct a header part in the name of header.php and construct the footer part by footer.php. Finally include all the content in one file.

    For example:

    header.php

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

    footer.php

    </html>
    

    So the final files look like

    include_once("header.php") 
    
    body content(The body content changes based on the file dynamically)
    
    include_once("footer.php") 
    
    0 讨论(0)
  • 2020-11-29 03:07

    You can use this for header: Important: Put the following on your PHP pages that you want to include the content.

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

    You can also include a navbar globaly just use this instead:

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

    In 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> 
    

    Do Not close Body or Html tags!
    Include html here:

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

    Footer.php:

    Code here:

    <?php
    //code
    
    ?>
    

    Navbar.php:

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

    Benifits:

    • Cleaner main php file (index.php) script.
    • Change the header or footer. etc to change it on all pages with the include— Good for alerts on all pages etc...
    • Time Saving!
    • Faster page loads!
    • you can have as many files to include as needed!
    • server sided!
    0 讨论(0)
  • 2020-11-29 03:08

    Besides just using include() or include_once() to include the header and footer, one thing I have found useful is being able to have a custom page title or custom head tags to be included for each page, yet still have the header in a partial include. I usually accomplish this as follows:

    In the site pages:

    <?php
    
    $PageTitle="New Page Title";
    
    function customPageHeader(){?>
      <!--Arbitrary HTML Tags-->
    <?php }
    
    include_once('header.php');
    
    //body contents go here
    
    include_once('footer.php');
    ?>
    

    And, in the header.php file:

    <!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>
    

    Maybe a bit beyond the scope of your original question, but it is useful to allow a bit more flexibility with the include.

    0 讨论(0)
  • 2020-11-29 03:09

    the simpler, the better.

    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> 
    

    the actual static html pages stored in the txt folder in the page.htm format

    0 讨论(0)
提交回复
热议问题