Use PHP Includes to Streamline Website Design

Leave your reply

Introduction

PHP includes allow you to separate the design of your website from the content. If there are parts of your website which are repeated on multiple pages, like a navigation bar, you can put this in one single file. Editing that file will then instantly update the content on all of the pages on which it is included.

Requirements

  • A Cloud Server running CentOS 7 or Ubuntu 16.04
  • PHP installed and running.

Note: PHP is installed and running on a Standard installation by default. If your server was created with a Minimal installation, you will need to install and configure PHP before you proceed.

Configure Apache to Parse HTML Files as PHP

The Apache web server will automatically parse any file with a .php extension as PHP code. However, if you have existing web files which have .html extension, you can configure Apache to parse these as PHP, as well.

This will not affect any HTML pages which do not contain PHP code. It will only allow Apache to execute PHP code, if it is found inside the HTML files.

Ubuntu 16.04

Open the /etc/apache2/mods-enabled/php7.0.conf file for editing:

sudo nano /etc/apache2/mods-enabled/php7.0.conf

Add the following to the top of the file:

<FilesMatch ".+\.html$">
    SetHandler application/x-httpd-php
</FilesMatch>

Save and exit the file. Restart Apache for the changes to take effect:

sudo systemctl restart apache2

CentOS 7

Open the /etc/httpd/conf/httpd.conf file for editing:

sudo nano /etc/httpd/conf/httpd.conf

Scroll down to the AddType block:

# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz

Add the following two lines to this block:

AddType application/x-httpd-php .htm 
AddType application/x-httpd-php .html

Save and exit the file. Restart Apache for the changes to take effect:

sudo systemctl restart httpd

Create a PHP Include

As an example, we will begin with an HTML file that reads:

<html>
<body>

<p>Hello world!</p>

<p>Welcome to the website.</p>

</body>
</html>

For this tutorial we will be putting the first line of text <h1>Hello world!</h1> into a separate file, then including it in our main HTML file.

Create a file include-me.php in your webspace and open it for editing. For example, to create the file in /var/www/html the command is:

sudo nano /var/www/html/include-me.php

Put the following into this file:

<p>Hello world!</p>

Save and exit the file.

Note: For security reasons, it is considered "best practice" to create these include files with a .php extension. That way if someone access the file directly in a browser, they will only see a blank page.

Next, create a file welcome.html in the same directory. For example, to create the file in /var/www/html the command is:

sudo nano /var/www/html/welcome.html

Put the following into this file:

<html>
<body>

<?php include('include-me.php') ?>

<p>Welcome to the website.</p>

</body>
</html>

Save and exit the file.

As you can see, we replaced the original line with a PHP include statement:

<?php include('include-me.php') ?>

This specifies that the contents of the include-me.php file, which is in the same directory, will be included at that point in the file.

If you view the welcome.html file in a browser, it will read:

Hello world!

Welcome to the website.

You can include the include-me.php file in as many other HTML files as you wish. Then, when you want to update the text in that section, you only need to edit the single include-me.php file once, regardless of the number of other pages on which it appears.

This can not only save you future editing time, it can also streamline the design of each individual page. For example, if you put the common headers, navigation, and footers in PHP include files, then each page only needs to include the content specific to each page (as well as PHP include statements for the common files).

Including Files From Other Directories

The file path of the PHP include statement is relative to the page in which it appears.

For example, it is common practice to put all PHP include files into the same directory for ease of organization. Let's imagine you create a directory includes inside the /var/www/html directory, and move the include-me.php file there.

Next you will need to update the welcome.html file so that it points to the new location of the include-me.php page. There are two ways to do this:

Relative File Path

If you choose the relative file path, you will need to change the PHP include statement to:

<?php include('includes/include-me.php') ?>
  • Advantage: This method makes it easier to migrate a website. As long as the web directory structure is preserved, this method will work on any server.
  • Disadvantage: The PHP include statement will need to be changed slightly for files in other directories. The more elaborate the directory structure, the more confusing this can get.

Use ../ to include a file which is one directory above the current file:

<?php include('../include-me.php') ?>

If the file is up two directories, you can use multiples:

<?php include('../../include-me.php') ?>

If the file is up two directories and down another one:

<?php include('../includes/include-me.php') ?>

And so forth.

Absolute File Path

If you prefer to specify an absolute file path, you will need to change the PHP include statement to:

<?php include('/var/www/html/includes/include-me.php') ?>
  • Advantage: The PHP include statement is the same regardless of where the HTML file is located.
  • Disadvantage: If the website is migrated to a server with a different directory structure, or to a different directory on the server (for example, from /var/www/html to /var/www/example.com/html), all of the PHP include file paths will need to be changed.