Hosting Fanatic

Lunarpages answers the phone when you call them!
Adam Sullivan - Friday 13th of June 2008 03:35:36 AM

Manage Content With PHP Includes

PHP Includes Basics

Have you ever had to makes changes on a site that uses no includes and no external CSS file? What about a site with several thousand pages? Do you fully understand what this can do to a developers mental sanity? If not then you probably really don't want to know. It's not pretty.

There is a way to alleviate this drive for mental breakdown though. Server side includes allow you to reuse pieces of your web pages by putting the pieces in seperate files and then including them into the web page on the server side. There are many different types of server side includes. We will talk about PHP includes.

Server Side What? PHP?

PHP is a scripting language that works on the server side. When you request a web page with your browser you are requesting the page from a big computer with all the files for whatever web site you are requesting the page from. This is called a server because it serves up web pages and other files. Because the server gets to handle the web page before it gives it to you it can do whatever it wants to the web page.

Server side includes allow the server to take pieces of a web page that get reused throughout the site and put them in the spots they need to be in the web page you are requesting before the server gives your browser the page. An example below...

<div style="border:1px solid black">
This is a file containing the top
     file: header.html
</div>

Now here's the page we are requesting through our browser...

<?php include("header.html");?>
This is the content on the web page

When the page containg the content is requested the server will take header.html and put it in place of include("header.html");. This way all of the pages on the web site can use include("header.html"); in place of the top portion of the web page. You can edit the header.html file then and change the top of every page on the entire web site!

You can do this as many times as you want on one page as long as your web host supports PHP. You could for example do the following...

<?php include("header.html");?>
<table>
<tr>
<td><?php include("leftnav.html");?></td>
<td>
All content goes here.
This file is alot smaller
  then it should be.
When the server gets a
  request for this file
  it will fill in the includes
  and return the full web page.
</td>
<td><?php include("rightnav.html");?></td>
</tr>
<?php include("footer.html");?>

That's all there really is to it. Proper use of includes can make a static HTML site alot easier to manage.

Blink   Del.icio.us   Digg   Furl   Google   Simpy   Spurl   Y! MyWeb