Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a PHP header/footer

Tags:

php

header

footer

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. Problem is, I'm pretty much totally unfamiliar with how php works. Is there a simplistic way to do this? I've seen a few answers for how to make a php header but they all seem different and I haven't had much success. I'm not saying they don't work (I probably did it wrong) but the simpler in this case, the better.

Thanks!

like image 329
rjwctm21 Avatar asked Nov 08 '11 17:11

rjwctm21


People also ask

How to include header and footer in a PHP file?

Just create the file header.php and use the get_header () function to include it in your index.php. To include your footer.php you can use the get_footer () function. Show activity on this post.

What is footer in WordPress?

What is footer : Footer section is website’s lower bottom section which contain its copyright information, some times top level horizontal menus. What we are doing in this tutorial : We are creating three files header.php, footer.php, mainPage.php .

How to create a fixed footer in HTML?

How To Create a Fixed Footer Example <style> .footer { position: fixed; left: 0; bottom: 0; width: 100%; background-color: red; color: white; text-align: center; </style> <div class="footer">

When should I call the PHP header?

This header hence must be called before any other output is been sent either by usual HTML tags, blank lines or from PHP. A few common mistakes are to read the code with include, access or any other require functions, having spaces or empty lines which are output before calling the header ().


3 Answers

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.

like image 50
William Avatar answered Oct 23 '22 09:10

William


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

like image 41
Ben Avatar answered Oct 23 '22 08:10

Ben


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") 
like image 3
Mohan Shanmugam Avatar answered Oct 23 '22 08:10

Mohan Shanmugam