PHP includes are essentially for including data from one file into another; they are also very handy at making your website easier to maintain.
If you have more than 4-5 pages in your website, you’ve probably already had to go through the trouble of editing every single one when you discover a common problem or when you decide it’s time to give your site a revamp. If you’ve nodded yes to this — you need includes. (This tutorial assumes your server supports PHP.)
First things first, you need to change the extension of every one of your web page files. That simply means all those files ending in .html, .htm or .shtml need to be changed to .php. For example: index.php, domain.php and so on. You also need to separate your style (layout information) from your content. If you don’t know how; read my tutorial on Separating Style from Content.
Here’s an example of what your page might look like — the “style” coding, as well as other repeated data is in bold:
<html> <head> <title>web page title</title> </head> <body> <table border="0"> <tr><td> <h1>welcome to my website</h1> <p>welcome to my website! this site is really awesome and you can find my content all over. the navigation is on the right and my friends are linked there too.</p> <h2>updates</h2> <p><strong>3.4.05</strong> - today i updated my reviews page!</p> </td></tr> </table> </body> </html>
Copy all the coding above the content into a file and save it as header.php, then copy the coding below the content into a file and save it as footer.php. You can now remove the repeated/style data from every other file you have — leaving behind only the content (the middle stuff).
At the top of every file place this:
<?php include('header.php');?>, and at the bottom:
Save the pages, delete the old ones, upload the new ones and then that’s it.. easy peasy, simple as pie!
A common misconception of PHP includes is that they can only be used to control “headers” and “footers”. This is not true — you can use PHP to include any file into any PHP file in any location. For example, if you have a business address that you want to include in certain pages, you could save your address in a file called address.inc and call it like so:
Note: I’ve used the .inc file extension in the address example — this is a common file extension for include files. DO NOT use .inc to store important data such as passwords and MySQL connection strings as .inc files display as plain text in the browser.