Tuesday, 13 April 2010

Using PHP Includes

Are you tired of having to copy and paste long codes into every single page in your website? For pages that contain short contents, it is alright if you just use HTML. But imagine if you have all those extra codes (for sidebar, advertisements, credits, etc.) wouldn't that be troublesome to include on every single page? And if there is a line of code that you want to change, you will have to change the same code on the other pages too.

Now, wouldn't you want to know how to do it the short and simple way? If you do, here's a tutorial on how to do it. Mind you that this tutorial will only provide the basic of PHP includes usage.

First of all, take a look at this basic HTML coding. Let's say it is the coding for owner.html:

To use PHP includes, basically, you will need to split your HTML codes and put them in separate pages and then join them together with this code:


Step 1:

Open up your text editor and put in your html codes. Remember to paste only the codes that start from <HTML> until <body>. The rest that comes in between the <body></body> tags is the content.

Save this file as header.php

Step 2:
Create a new file and name it footer.php. Paste the bottom part of the HTML into this file:


Step 3:
Paste the contents of your page into a new file and name it owner.php.

Step 4:
Now, it is time to connect the 3 files together.
In the owner.php, paste this code at the very top of the file.
And at the very bottom of the file, paste this code:
Once you have coded that page. Upload the three files to your site and test it out. Go tohttp://yoursite.com/owner.php and see if it works.

Note: Make sure you place the header.php and footer.php in the same folder as the page that you want to connect them to otherwise it will not work.