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. We will be making use of a PHP statement:
<?php include(); ?>
[Mind you that this tutorial is only providing one example on how the include() function can be used.]
First of all, take a look at this simple HTML coding. Let’s say it is the coding for owner.html:
<html> <head> <title>your website title</title> </head> <body> <h1>welcome to my page</h1> <p>Hi there! This page is about me. My real name is bla bla bla. I am bla bla bla years old.</p> <h2>More about me</h2> <p>I like bla bla bla. I don't like bla bla bla.</p> </body> </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 the include() function.
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.
<html> <head> <title>your website title</title> </head> <body>
Save this file as header.php
Create a new file and name it footer.php. Paste the bottom part of the HTML into this file:
Paste the contents of your page into a new file and name it owner.php.
<h1>welcome to my page</h1> <p>Hi there! This page is about me. My real name is bla bla bla. I am bla bla bla years old.</p> <h2>More about me</h2> <p>I like bla bla bla. I don't like bla bla bla.</p>
Now, it is time to connect the 3 files together.
In the owner.php, paste this code at the very top of the file.
<?php include('header.php'); ?>
And at the very bottom of the file, paste this code:
<?php include('footer.php'); ?>
Once you have coded that page. Upload the three files to your site and test it out. Go to http://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. If you want to store the header and footer files in a different folder, you will need to replace the paths for both header and footer.