php website header body footer

How to Make a PHP Website Header, Body, and Footer ? Learn in Minutes

If you’re learning PHP, you should know how to make a PHP website header, body, and footer.
It’s one of the first steps to build a clean and professional website.

This simple setup keeps your pages neat and saves you from writing the same code again and again.
You can change one file, and it updates on all pages easy, right?

Many beginners skip this part and end up with messy code.
But once you learn how to separate the header, body, and footer, everything becomes faster and more organized.

In this guide, I’ll show you step by step how to build them the right way.
Even if you’re new to PHP, don’t worry you’ll understand every part clearly.

By the end, you’ll have a smart, reusable layout that works like a pro website.

Every professional website follows this pattern:

  • Header: contains the navigation bar, logo, stylesheets, and meta tags.
  • Body: holds the main content that changes page to page.
  • Footer: includes closing tags, copyright, and contact links.

This structure helps keep your design consistent across all web pages. For example, if you want to change your logo, you only update it once in the header.php file and it changes on every page!

Another big benefit is readability and maintenance. Developers or SEO professionals who join your project later will easily understand your code layout.

And search engines like Google also prefer websites that follow semantic and well-organized HTML structure.

If you’re building your site from scratch, I highly recommend checking this helpful guide on Chancely Website Building it explains how beginners can start building websites step by step using modern methods.

php websites

Let’s understand what each section does.

Header

The header sits at the top of your page. It usually contains:

  • Your logo
  • Navigation menu
  • Meta tags and stylesheets
  • The start of your <body> tag

Example:

<!-- header.php -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?= htmlspecialchars($pageTitle ?? 'My PHP Site') ?></title>
  <link rel="stylesheet" href="/css/style.css">
</head>
<body>
  <header>
    <a href="/">MySite</a>
    <nav>
      <a href="/index.php">Home</a>
      <a href="/about.php">About</a>
      <a href="/contact.php">Contact</a>
    </nav>
  </header>

Body (Main Content)

This is where each page’s unique content goes.
It sits between your header and footer includes.

Example:

<!-- index.php -->
<?php
$pageTitle = 'Welcome to My Site';
include __DIR__ . '/header.php';
?>
<main>
  <h1>Welcome!</h1>
  <p>This is the home page of our PHP layout example.</p>
</main>
<?php include __DIR__ . '/footer.php'; ?>

Your footer closes the page.
It’s a great place for:

  • Copyright text
  • Footer navigation
  • Contact info
  • Scripts

Example:

<!-- footer.php -->
  <footer>
    <p>&copy; <?= date('Y') ?> My PHP Website. All rights reserved.</p>
  </footer>
  <script src="/js/main.js" defer></script>
</body>
</html>

Tip: Always load non-essential JavaScript in the footer to improve page speed.

Folder Structure You Should Use

Here’s how your project folder should look:

/project
  /partials
    header.php
    footer.php
  /css
    style.css
  index.php
  about.php
  contact.php

Keeping header and footer inside a partials folder makes your project tidy.
This helps you quickly find reusable components especially when your site grows.

Now that your files are ready, it’s time to connect them.

In PHP, we use the include or require function.
Here’s the difference:

  • include → shows a warning if the file isn’t found but keeps running.
  • require → stops execution if the file isn’t found.

Example:

<?php
include __DIR__ . '/partials/header.php';
?>
<main>
  <h1>About Us</h1>
  <p>This content comes between header and footer.</p>
</main>
<?php
include __DIR__ . '/partials/footer.php';
?>

You can also use include_once or require_once to avoid loading the same file twice.

What if you want different titles on each page?

Easy just create a variable $pageTitle before you include the header:

<?php
$pageTitle = 'Contact Us';
include __DIR__ . '/partials/header.php';
?>

Now your <title> tag in the header will automatically show each page’s title.

You can even use a variable for active navigation links:

<nav>
  <a href="/index.php" class="<?= $activePage === 'home' ? 'active' : '' ?>">Home</a>
  <a href="/about.php" class="<?= $activePage === 'about' ? 'active' : '' ?>">About</a>
</nav>

This tells users (and search engines) which page they’re on great for SEO and usability.

What is the header() Function in PHP?

Don’t confuse header() with your HTML header file.
header() in PHP is used to send HTTP headers, like redirects or caching instructions.

Example:

if (!isset($_SESSION['user'])) {
  header('Location: /login.php');
  exit;
}

It must appear before any HTML output, otherwise you’ll get an error.
That’s because once the browser starts receiving HTML, PHP can’t send HTTP headers anymore.

What Does $_POST Do in PHP?

Whenever someone submits a form on your site, the data they enter (like their name or email) is sent using the POST method.

PHP collects that data in a global variable called $_POST.

Example:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $name = htmlspecialchars($_POST['name']);
  echo "Hello, $name!";
}

Always sanitize user input with htmlspecialchars() to prevent security issues.

If you have a contact form, this is how your PHP will handle it.
You can see an example of this on our Contact Us page.

Make It Accessible and SEO-Friendly

Using semantic HTML isn’t just fancy it helps search engines and users.

Here are quick tips:

  • Use <header>, <main>, and <footer> tags correctly.
  • Add a “Skip to content” link before navigation for keyboard users.
  • Include aria-labels on navigation elements.
  • Keep your <title> and <meta description> unique for each page.

Search engines love semantic layouts.
They make your site easier to crawl and understand.

Your website must look good on mobile.
Use media queries in your CSS:

@media (max-width: 768px) {
  nav a {
    display: block;
    margin: 8px 0;
  }
}

Keep the header light avoid large images or heavy scripts.
You can even use a hamburger menu for smaller screens.

For performance, load your main CSS in the <head> and defer JavaScript in the footer.

Conditional Headers and Footers (Advanced Tip)

Sometimes, you may want different headers for different pages.
For example, a simple header on the login page and a full one on your home page.

You can do this:

if ($layout === 'simple') {
  include __DIR__ . '/partials/header-simple.php';
} else {
  include __DIR__ . '/partials/header.php';
}

This helps when you’re building larger sites or dashboards.

php websites

Performance & Best Practices

Here are quick performance boosters for your PHP layout:

  • Use include_once to avoid duplicate includes.
  • Minify CSS and JS.
  • Cache your header and footer if they don’t change often.
  • Compress images before uploading.
  • Use a reliable hosting platform for faster response time.

Remember, Google loves fast websites it’s a ranking factor.

Common Mistakes to Avoid

  1. ❌ Writing output before using header() function.
  2. ❌ Using wrong file paths — always use __DIR__ for accuracy.
  3. ❌ Forgetting to close your <body> and <html> tags.
  4. ❌ Including heavy scripts in the header.
  5. ❌ Not validating form data before using $_POST.

A simple troubleshooting trick:
Enable error reporting in your PHP file while developing.

ini_set('display_errors', 1);
error_reporting(E_ALL);

This will help you find errors instantly.

Example Mini Project

Let’s create a small project that uses everything we learned.

Goal: A simple 3-page PHP site (Home, About, Contact).

Files:

index.php
about.php
contact.php
partials/header.php
partials/footer.php
css/style.css

Each page:

<?php
$pageTitle = 'About Us';
$activePage = 'about';
include __DIR__ . '/partials/header.php';
?>
<main>
  <h1>About Our Project</h1>
  <p>We help beginners learn PHP the easy way.</p>
</main>
<?php include __DIR__ . '/partials/footer.php'; ?>

Now if you update the logo or link in header.php, it instantly updates across all three pages.

That’s the power of reusable PHP templates.

What Content Normally Resides in a Footer PHP File?

A good footer contains:

  • Copyright notice
  • Contact details
  • Navigation or quick links
  • Privacy policy link
  • Social media icons

You can also add Google Analytics, scripts, or cookie consent code in your footer to ensure they load last and don’t block the main content.

For updated practices, check W3Schools PHP Include Tutorial it’s a trustworthy external resource with current examples.

Quick FAQs

Q1: How to include header PHP in PHP?
Use include __DIR__ . '/partials/header.php'; — it keeps file paths consistent.

Q2: How to create a header and footer in a website?
Create header.php and footer.php, then include them in each page.

Q3: What is footer in PHP?
It’s a separate file that holds your site’s closing HTML section.

Q4: What can PHP do with the header() command?
It sends HTTP headers—used for redirects or cache control.

Q5: What does $_POST do in PHP?
It collects form data sent via POST method.

Q6: Which content normally resides in a footer PHP file?
Copyright, navigation, analytics, and scripts.

Q7: How to edit footer in PHP?
Just open footer.php, make your change, and save it updates across all pages.

Q8: Where can header and footer be found?
Usually in a folder named /partials/ or /includes/.

Why This Approach Helps SEO

Search engines prefer structured, semantic HTML and fast-loading pages.
Using PHP includes for your header and footer reduces duplication and improves consistency.

You’ll spend less time editing and more time creating new content—
which helps your site stay active and fresh in Google’s eyes.

Final Thoughts

Building a PHP website with header, body, and footer isn’t complicated once you know the basics.
With just a few includes, you can manage your layout like a pro faster, cleaner, and more scalable.

You’ve now learned:

  • What each section does
  • How to include them
  • How to make them dynamic and responsive
  • How to fix common errors

Keep practicing.
The more you experiment with PHP includes, the more powerful your websites will become.

Soon, you’ll not only save time but also build sites that Google and users both love.

Picture of Ambreen Basit

Ambreen Basit

Ambreen Basit is a blogger and SEO content creator who helps people grow online with smart, easy-to-understand tips. Follow her for branding, blogging, and ranking insights

Leave a Reply

Your email address will not be published. Required fields are marked *