Using PHP Includes To Work Smarter Not Harder

Source : http://www.netpond.com
Author : haui
Published on : December 08, 2006


  
haui's Profile and details
Hi Folks, we bring you here tons of tutorials writen by great webmasters and collected them over at Netpond.com

Tutorial: Using PHP Includes To Work Smarter Not Harder.
By -- REßEL


In the old days of the web, frames were used to combine pages so that you only needed one navigation page or footer. Most hosting packages these days come with php. You can use PHP to "combine" several pages into one. And even have them as a standard template for your site building purposes that you add design to later.

1: In your text editor or wysiwyg of choice create the main page of your site "index.php"

2: Paste the following line into a blank document.
<h1>Header</h1>
Save as header.html

3: Paste the following line into a blank document.
<p>Sales Text Goes Here<br /><a href="#" title="keyword title">Sales Link Here</a></p>
Save as ad1.html

4: Paste the following line into a blank document.
<a href="#" title="keyword home">Home</a><br />
<a href="#" title="keyword page1">Page 1</a><br />
<a href="#" title="keyword page2">Page 2</a><br />
<a href="#" title="keyword page3">Page 3</a><br />
<a href="#" title="keyword page4">Page 4</a><br />
<a href="#" title="keyword page5">Page 5</a><br />
Save as navigation.html

5: Paste the following line into a blank document.
<p>Sales Text Goes Here<br /><a href="#" title="keyword title">Sales Link Here</a></p>
Save as ad2.html

6: Paste the following line into a blank document.
<a href="#" title="link1 title">Link 1</a><br />
<a href="#" title="link2 title">Link 2</a><br />
<a href="#" title="link3 title">Link 3</a><br />
<a href="#" title="link4 title">Link 4</a><br />
<a href="#" title="link5 title">Link 5</a><br />
Save as links.html

7: Paste the following line into a blank document.
<p>Sales Text Goes Here<br /><a href="#" title="keyword title">Sales Link Here</a></p>
Save as ad3.html

8: Paste the following line into a blank document.
<a href="#" title="home title">Home</a> | <a href="#" title="privacy title">Privacy</a> | <a href="#" title="terms title">Terms</a> | <a href="#" title="site map title">Site Map</a> | <a href="#" title="contact title">Contact</a>
Save as footer.html

9: Open your index.php file and paste the following between the body tags.
<?php include("header.html"); ?>
<?php include("ad1.html"); ?>
<?php include("navigation.html"); ?>
<?php include("ad2.html"); ?>
<?php include("links.html"); ?>
<?php include("ad3.html"); ?>
<?php include("footer.html"); ?>

10: Upload to server to check everything is working.

You now have a very basic php includes site that you can add design, formatting and content to.
Basic Template example: http://www.adult-webmasters-resourc...s/php-tutorial/
Basic Template Using 2 Column Table: http://www.adult-webmasters-resourc...orial/table.php

To create the above you just put the <?php include("filetobeincluded.html"); ?> commands where you want them to appear - In other words the navigation.html, ad2.html and links.html would go in the left table cell and the header.html, ad1.html, ad3.html and footer.html would go in the right table cell, and your content would be put between the two ad.htmls as if making the page as normal. You can make them as complex or as simple as you like. It all depends on the html/scripts/graphics etc you put into the individual includes. I've just used simple basic examples. You can use as many or as few includes as you need.

You can now edit the individual html pages to suit. Using includes on a multi-page site means only having to edit once to make the change on all the pages using the include. Want to swap out the sponsor in ad1.html? Change the html in that file and all the pages using it will reflect the change.

You now know how to use PHP includes to use one file on multiple pages.