Includes Guide
Introduction
Includes are designed to make web scripting easier by using a simple include line to insert as much code as you like. Includes are mainly used with code that is the same on every page of a website. There are two widely-used types of includes:- PHP Includes
- PHP is a dynamic scripting language, and, like most scripting languages, it has an
includefunction. However, your host must support PHP and your webpages must have a.phpextension in order for it to work. - SSI
- SSI, which stands for Server Side Includes, is a simple scripting language whose main purpose is to include code. SSI is widely supported by hosts, but it only works with
.shtm(l)extensions.
Person #1
Person #2
You'll notice the only thing that changes between the two pages is the opinion, which I have bolded. Here are the two pages' code:
Person #1 (
ssi1.php)
<html> <head> <title>SSI Example</title> </head> <body> <h1>SSI Example Page</h1> <p>This is an example page. Example pages are meant to exemplify something. Below is someone's opinion of example pages:</p> <p>"I love example pages! :)"</p> <p>I hope this page has enlightened you. Please visit again.</p> </body> </html>Person #2 (
ssi2.php)
<html>
<head>
<title>SSI Example</title>
</head>
<body>
<h1>SSI Example Page</h1>
<p>This is an example page.
Example pages are meant to exemplify something.
Below is someone's opinion of example pages:</p>
<p>"I hate example pages! :("</p>
<p>I hope this page has enlightened you.
Please visit again.</p>
</body>
</html>
Notice that the only thing that changes between the two pages is the opinion. What if you wanted to make fifty more of these pages? You'd have to copy and paste everything, even though the only thing that changes is the opinion. But what if you wanted to change another part of the code? You'd have to manually edit every one of the fifty pages.
To start, we place everything before the changing code into a separate .php file. (Note that the file could also be .txt, .htm, .html, etc.) For the purpose of our example, we'll call this file before.php. Thus, the content of before.php is:
<html> <head> <title>SSI Example</title> </head> <body> <h1>SSI Example Page</h1> <p>This is an example page. Example pages are meant to exemplify something. Below is someone's opinion of example pages:</p>
Now, let's take everything after the changing code and put it into another separate .php file. We'll call it after.php.
<p>I hope this page has enlightened you. Please visit again.</p> </body> </html>
Inserting the Include Code
Now, depending on whether you need PHP Includes or SSI, replace the code you placed in the.php files with the following.
Inserting SSI
Replace the top part with:<!--#include virtual="before.php"-->Replace the bottom part with:
<!--#include virtual="after.php"-->Also, if you're using SSI, your host must support it and you must change the file extension of your page from
.htm(l) to .shtm(l), or it won't work.
Inserting PHP include
Replace the top part with:
<?php include('before.php');?>
Replace the bottom part with:
<?php include('after.php');?>
You must change the file extension of your page to .php if you haven't done so already. Also, remember, your host must support PHP.Conclusion
Let's look back at our two example pages. Here's what the code for the pages should look like:Person #1 (
ssi1.php)
<?php include('before.php');?>
<p>"I love example pages! :)"</p>
<?php include('after.php');?>
Person #2 (ssi2.php)
<?php include('before.php');?>
<p>"I hate example pages! :("</p>
<?php include('after.php');?>
Now, when the server processes the include function or the SSI, it looks for the file named in the function and inserts its code automatically. That means that you just have to edit before.php or after.php and it will change wherever you put the SSI or include function.





