require footer and header each page but error 500


I start in PHP and I create my website. I create a component of header and footer to print on each page with : require '/assets/components/header.php'; and require '/assets/components/footer.php';

So, the problem is that on localhost, it works, but on website, it didn’t work and i have a http error 500

Repertory :
Folder tree

I try :

1. The different type of link

require 'assets/components/header.php';

require './assets/components/header.php';

require '../assets/components/header.php';

2. create a variable $BASE_URL to stock the url of the website and require like this : require "$BASE_URL/assets/components/header.php";. I set the setting ‘allow_url_include’ to true but i have this error :

[04-Nov-2021 12:28:40 UTC] PHP Warning:  require(http://#####/assets/components/header.php): Failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error

‘#####’ -> my website

I read in a topic, that not secure to set ‘allow_url_include’ to true so if we can do different.


require_once "$BASE_URL"."assets/function.php";
    $title='Error 404 - Catif';
<!DOCTYPE html>
<html lang="fr">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href='/assets/css/style.css'>
    <title><?= $title ?></title>
        <div class="nav-left"><p class="nav-name">Catif</p></div>
        <div class="nav-right">
            <a class="nav-item <?php if($page === 'home'): ?>active<?php endif ?>" href="/index.php">Projets</a>
            <a class="nav-item ml-80 <?php if($page === 'me'): ?>active<?php endif ?>" href="views/me.php">Moi</a>
            <a class="nav-item ml-80 <?php if($page === 'contact'): ?>active<?php endif ?>" href="/views/contact.php">Contact</a>
        <button class="nav-button">==</button>

    <div class="container">

>Solution :

When including component files in PHP, you should be using file paths, not URLs. For example, we can use relative file paths to include the header in pageOther1.php, which should be like this.

require "../../../assets/components/header.php";
// OR
require __DIR__ . "/../../assets/components/header.php";

To make things more efficient, especially if you are going to include multiple components and have different levels of nested views, you could define the header and footer paths (and include any other global scripts like functions.php) in a PHP file (e.g. initialize.php or config.php) on the root directory of your app. That way you only need to require initialize.php and include the components you want using PHP constants.

Here’s a more efficient approach

Define the component paths as a constant in a PHP file (e.g. initialize.php) on the root directory

define("APP_PATH", dirname(__FILE__)); // The main project directory
define("HEADER", "APP_PATH" . "/assets/components/header.php"); // Path to header.php
define("FOOTER", "APP_PATH" . "/assets/components/footer.php"); // Path to footer.php

// You could also include your global scripts here and don't have to include it on each view page
require "APP_PATH" . "/path_to_global_script.php";

Then include the components and scripts in a view. An example with pageOther1.php will be:

require "../../../initialize.php"; // Path to the file which defines the constants
require HEADER;
// Page content
require FOOTER;

You can easily extend this for multiple components and function scripts.

Leave a Reply