Including Files
Why Include?
If you have ever built a site over three pages, you will have a very easy time understanding the value of including files in your pages vs. hard-coding everything.
Let's say you have a header on every page. In that header, you have your web site logo and name along with the current time in am pm format and the date. Let's assume for the sake of argument that you hard-coded the header into each page. Suddenly, you want the time to display in a 24 hour format instead of an am pm format. Now you have to go into each page and change the code to reflect the format change. This would probably be fine if you only had three pages, but what about if you had 30 pages. Sure there's copy and paste, but that tiny change just cost you about an hour. Herein lies the beauty of including pages.
If you had had an include page with the header in it, you would have only had to change the code once. All the pages that would have included that header would be updated automatically.
Server Side Includes
This is used by apache to server parse files and execute any commands in your pages. Your server will need to be configured to use SSI or you will have to add directives to your .htaccess file. There are many complications to using SSI and is a general pain to deal with. You have a limited number of directives you can use and if you need to use PHP or ASP alongside SSI, you will notice a performance hit.
A far more powerful implementation is to use a server side scripting language like PHP or ASP. If none of these options are available to you, you can still include code using Javascript.
PHP Includes
Using PHP to include files is easy. I use php on almost all my pages, so every page is being parsed anyway. If I want to add some php script to the included file, I do. You can write the HTML in a file, name it whatever you want, and then use this command to "inject" the html into your code
includedFile.txt(the contents of the file to include):
<p>Include this paragraph</p>
The include statement in your html:
<?php include("relativePathToFile/includedFile.txt"); ?>
Basically, what happens is that whatever HTML code you have written is written right into your page before the document is sent to the client. You could include a snippet of HTML, a result set from another PHP parsed page, just about anything. The thing you need to remember is that the page you put this include statement in has to be a page parsed by PHP. You can do this in one of two ways. Make the file have a .php extension on the end of it or add this line to your .htaccess file:
AddType application/x-httpd-php .html
You can replace the .html with .htm or whatever extension you are using for your pages. If you use .htm and .html extensions, just make a separate entry for each extension type:
AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm
One note of caution when using this approach is that every file with an extension that you add is going to be parsed by the PHP parser before being sent out. You can see how this will affect your site's performance if pages are being parsed unnecessarily. It's not really noticeable for the most part, but it can be.
One more little trick is that you don't have to have an entry in your .htaccess file for the included files. If you name all your included files with a .inc extension, then just wrapping your PHP code in the included file with <?php ?> will cause that code to be parsed. Warning: If you name your include files with an extension like .inc or whatever you dream up and don't include this as a file to be parsed by PHP, anyone can view the PHP code in your file by typing in the file's URL directly. Not a big deal if you have only HTML or some variables you don't care if people see, but a big deal if you have a database password in a file.
The reason all this works is because PHP is already parsing your page. It basically takes your included file and replaces your include statement with the included file's contents.
What about absolute references to those files? An include file cannot be written with an absolute reference since you could then find out the variables of any script from any site. Great if you're a hacker, not so great if you own the script. This is a server setting and it should be set to disallow including from outside the server. So how do you access an include file from a script then if you can only use relative references and you need to access the file from various directories on your server? Assuming you don't want to re-code every time you move a file or include from a different directory, dynamically write the relative URL. Here is a solution using the $PHP_SELF variable and finding out where you are relative to the root of your web site and then dynamically writing the relative path to the include file:
<?php
$includePath = '';
//start with symbolic path to the root directory
for($i=1;$i<preg_match_all('[\/]', $_SERVER['PATH_INFO'],$a);$i++)$includePath
.= '../';
//add relative path to file from root directory next
$includePath .='pathFromSiteRoot/includeFile.php';
$includePath = realpath($includePath);
//include the file
if($includePath !== false)include($includePath);
?>
Javascript Includes
Javascript can be very effective at including snippets of code in your document. First off, the server doesn't have to do any work if you don't want it to since the client's browser is executing the javascript code. Secondly, you can include dynamic content based on the environment. If you want to include a snippet of html into your page, just create a javascript file and use the document.write() command to write the html into your document.
Once you have done that, you can insert a script anywhere in your document. Just use this html code to include the javascript file that will be writing the code:
<script type="text/javascript" src="pathToYourFile/yourFile.js"></script>
Then in your yourFile.js script you could put some simple code like this in it:
<!--
document.write('<p>Insert this paragraph.</p>');
//-->
The obvious disadvantage here is that users will have to have their javascript enabled. Most people do so you don't have too much to worry about, but remember that some won't and your code won't execute. A safe bet is to use javascript when the information isn't critical to the document or you have backup provisions elsewhere on the page.
