How To PHP Includes (AKA SSI's) Tutorial
PHP Includes are one of the best tools there are! They are extremely easy to implement, and make any future editing a total breeze. However, to a novice they can be a bit confusing. Below is a very easy step-by-step process to use includes on your site.
Lets assume that you have this code below sitting in your index.html page. We are going to take part of this code out of your index.html page and replace it with a php include. Note: any page using a php include must end in a .php extension (simply replace all .html extensions with a .php file extention)! I'll make it as simple as possible - ready? - lets go!
<ul id="nav"> <li><a href="http://www.yourdomain/home">HOME</a></li> <li><a href="http://www.yourdomain/about">ABOUT</a></li> <li><a href="http://www.yourdomain/contact">CONTACT</a></li> </ul>
Step 1) Within your site, create a folder called includes
Step 2) Within this folder create a file called nav.inc Note: this file extension can also be a .php or a .txt (e.g. nav.php or nav.txt), but for simplicity sake, let's keep it a .inc extention.
Step 3) With your cursor, highlight and cut out just the <li's>. Yes, leave the <ul> in place! Reason being, if you only use the li's (not the ul also) you can reuse the same include somewhere else in your site (e.g. #footer), and simply style it differently with a unique id on each ul.
Step 4) Now, paste this code into your new file you created named nav.inc. Note: Only place this raw code onto this particular page, no other code! No doctype, <html>, <body> tags, or etc. Your nav.inc page should now look like this.
<li><a href="http://www.yourdomain/home">HOME</a></li> <li><a href="http://www.yourdomain/about">ABOUT</a></li> <li><a href="http://www.yourdomain/contact">CONTACT</a></li>
Step 5) Now in it's place, on your original page, insert the php include that looks like this.
<ul id="nav"> <?php include("includes/nav.inc"); ?> </ul>
Step 6) Save, load, and check it out! How easy was that uh?
Helpful Tip #1 Always use absolute links inside your php include files (e.g. http://www.yourdomain.com/about.php).
Helpful Tip #2 If your php include (i.e. <?php include("includes/nav.php"); ?>) is in a file within another folder - lets say it's sitting in folder named assets. Well, you need to point it at the includes folder right? Because thats where the nav.php file is sitting (or should be at least). So you need to go up one level and then back down into the includes folder. You do this by placing this ../ before the link like so (e.g. <?php include("../includes/nav.php"); ?>). If you needed to go up two levels, in other words come out of a folder within a folder, then you would use two of them before the link like so ../../, and so-on and so-on.
Need help adding this or other code to your website? Post it here in the forum!