Management is important when you are designing and developing any website and ‘include files’ offer a great way to keep your website easily updatable and maintainable.

In include file is a way of placing a chunk of repeatedly used code into its own separate file and adding a reference to it whenever you need to. In my experience the most common uses are the navigation menu and the website footer.

I will run through how to do this in both PHP and Classic ASP. You can use the Classic ASP in method ASP.NET, but the framework offers ‘Master Pages’ which are fairly similar to includes files.


ASP

There are two types of ASP include, ‘Relative’ and ‘Virtual’

Relative

As you would expect, relative include files rely on a relative (absolute) file path. For example:

<!--#include file="includefile.inc"-->

In this case your browser will search for the include file within its current directory

<!--#include file="includes/includefile.inc"-->

Whereas in this case it will look for a folder named ‘includes’ within the current directory

<!--#include file="../includes/includefile.inc"-->

It will also work when searching for files in the parent directory

The problem with relative include files is that the file path cannot begin with a forward-slash, meaning you cant search the root directory if it is more than one folder above the current location.

Virtual

Virtual include files on the other hand allow you to reference the root folder. For example:

<!--#include virtual="/includes/includefile.inc"-->

In this case the browser will look for the include file by starting at the root location, searching for a folder named ’includes’ and then the file names ‘includefile.inc’.

PHP

The default PHP include files work in the same way as a relative ASP include file, meaning you cannot access the root, only the current or parent directory. For example:

<?php include("include-header.inc"); ?>

This is because PHP can look deeper into a file system than the root directory of your domain (usually a folder named ‘HTDOCS’). However, this can be solved by changing the PHP code slightly. For example:

<?php include($_SERVER['DOCUMENT_ROOT']."/include-header.inc"); ?>

Note. There are many methods that can be used to achieve this effect, but I have fount this to be the most concise and therefore the one I tend to use.

Notes on Include Files

In this article I have used the extension ‘.inc’ for include files, I use this on a daily basis for repeated HTML-based information.

However, an ‘.inc’ file can be browsed and viewed as plain text even if you add your ASP/PHP functions in there – so if you are using include files in this way then use standard ASP/PHP extensions and keep your server-side code hidden.


Related Posts

  1. Using Flash as a background

Posted in All, Tutorials.