Search code examples
phphtmlincludehtml-head

HTML / PHP: Which parts of an HTML head can be saved in a separate file


I am new to HTML and hope someone can help me with this.

I am trying to set up a website where the code for every page is saved as a separate php file. Since all these pages use the same document head and includes I would like to remove as much as possible of this from the pages and save it in a separate includes file (header.php) which I can then include using PHP.

My current head looks as follows and since this is the same on all pages I currently have ALL this saved in the separate header.php file and then just use <?php include "header.php"; ?> on the single pages.

Can someone tell me which parts of this should remain on the single pages for a proper setup and if anything should be changed or added here ?

Note: jQuery and JS are included separately in the footer.

My PHP (header file):

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta name="author" content="John Doe" />
        <meta name="description" content="Created: 2015-06" />

        <base href="http://www.MyURL.com" target="_self" />

        <!-- CSS -->        
        <link rel="stylesheet" type="text/css" href="includes/styles.css" />
        <!-- CSS - Font Awesome -->
        <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" />

        <title>My Page</title>
    </head>

    <body>
        <div class="wrapper">
        <!-- ... -->

Solution

  • Your Top head file (top_head.php):

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <meta http-equiv="X-UA-Compatible" content="IE=edge" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    
            <?php if(isset($page_author)){ ?>
                <meta name="author" content="<?php echo $page_author; ?>" />
            <?php } ?>
    
            <?php if(isset($page_description)){ ?>
                <meta name="description" content="Created: 2015-06" />
            <?php } ?>
    
            <!-- CSS -->        
            <link rel="stylesheet" type="text/css" href="includes/styles.css" />
            <!-- CSS - Font Awesome -->
            <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" />
    
            <title><?php echo $page_title; ?></title>
    

    Your current header file:

    <?php
          $page_title = 'Hello';
          $page_description = 'This is a beautiful description';
    ?>
    <?php include("top_head.php"); ?>
    </head>
    
        <body>
            <div class="wrapper">
            <!-- ... -->