Server Side Includes with PHP...
or how server side includes can save the world - a how to with examples
Resources - Tutorials and Articles
by Tad Coffin
MediaTitan.com
<? include("includes/yourfile.php");?>
There it is. A PHP include. An include, eh? What's that?
Glad you asked. If you could know only a few things about web programming, this is would be one of the better things to know.
A server side include, such as the include written in PHP above, allows many pages to be updated by a single file.
Example. You have a web site with one hundred and one HTML pages. You have a navigation menu that is on every page. You want to change the menu. If you created your one hundred and one web pages without using an include (or a database or frames) you would probably have to go and change all one hundred and one files. If you were using a server-side include, you would only have to change one file.
In other words, if you maintain a web site and are not using server-side includes already, you should start as soon as you finish reading this article. Imagine the time that is spent every day in the world updating pages that could have been updated by server-side includes. This time could instead be spent saving humanity from itself!
Lets get specific. Supposing your HTML page, called index.php, looks something like this:
<html>
<body>
<table>
<tr>
<td>This is my Menu<br>
<a href="index.php">Home</a><br>
<a href="page.php">Page</a><br>
<a href="page_2.php">Page 2</a><br>
</td>
<td>This is the main text of your page!
</td>
</tr>
</body>
</html>
If you wanted to use a server-side include for your menu, you would have two files. One would be called menu.php in a folder called includes (or anything you want to name your includes folder), and would look like this:
This is my Menu
<a href="index.php">Home</a>
<a href="page.php">Page</a>
<a href="page_2.php">Page 2</a>
And your index.php would look like this:
<html>
<body>
<table>
<tr>
<td><?include("includes/menu.php");?>
</td>
<td>This is the main text of your page!
</td>
</tr>
</body>
</html>
Now, all your pages can have different text and designs on them, and as long as you put the include code on each page, you can change every page just by changing the include file (menu.php in this case).
Go ahead and try it out yourself, then make sure from here on out you make your web pages using server-side includes. I don't make this stuff up. Don't believe me? Go straight to the source:
http://www.php.net/manual/en/function.include.php
If you would like a professional to help you with your web site, contact Media Titan today.
webmaster@mediatitan.com
619-223-8530
|