Beginner Tutorial on PHP Includes
Using PHP includes (sometimes referred to as SSI or Server Side Includes), can give you the flexibility to make sitewide changes to your pages and centralize common elements in one file. This will make the task of maintaining your website much easier. PHP includes are simple to create and use, you do not have to be a programming expert to take advantage of this powerful tool.
Simply put a PHP include is a way of putting content into an external location. Instead of repeating the same code in every page it will reside in one file. Whenever you want this particular content to appear on a page you will place a simple line of code in the appropriate location. When that webpage is visited via a web browser the server will have interpreted the include before it was displayed. If you were to view the source code you would not see the code that calls the PHP include, but instead you will see the actual code the PHP include is comprised of. Search engine crawlers will the see the page in the same way so this method is search engine friendly.
PHP includes are most commonly used for headers, footers, and links navigation, but there are many other uses to which they can be applied. If there is any element that you want to appear throughout your site or a particular section you can use this method. The actual content of your PHP include may be 100% straight HTML or a combination of PHP and HTML
First, in order to use an include file, you will need to name your pages using a .php extension. This will not affect any HTML coding in your pages as PHP and HTML work hand in hand together.
In this example you will learn how to use an include to create and control your footer.
- First create a new folder where you will store your incude. In this example we will assume that the folder will be named "common", but you may name it anything you like.
- Create a file using a text editor such as notepad. Never use editors like Word as they will make undesired changes to characters. Name the file footer.php.
- Place the code from your footer area into the file. This file should include everything you want to appear in your footer. This may be text, tables, graphics, links, or whatever elements you need. Save the file and upload it to the "common" subfolder.
- To test it out and see what it looks like edit your index.php. In place of the actual footer contents in your index file place the following line of code:
-
<?php
include( '/home/(User or Account ID Here)/public_html/common/footer.php' );
?> - Be sure to replace (User or Account ID Here) with your actual account ID.
- NOTE: This is the way the path will be formatted on most servers. If this path does not work for you contact your hosting company to find out the proper path to use.
Now that you see how easy it is to incoporate PHP includes into your website you will want to use them more. This method makes such as tasks as adding links to the footer or changing images, copyright dates, etc. a snap. Instead of having to go through and edit hundreds of pages you will only need to make changes to one file to see the changes take place throughout your entire site.
About the Author:
Esoomllub has been coding professionally for 20 years, taking up PHP coding in the last
6 or so years. He is now basically coding on his own sites, and only occasionally takes on
contract work. He is hoping his new blog will become a place to talk more about
website programming and webmaster revenues,
but it's not there yet!
