How to create a well structured website using PHP and include function (1 header and footer file)

In this example i’m going to show you how to create a very simple and easy website using PHP and the PHP Include function. This is a great way to create your website because you only need to have one header and one footer file! This is how i structure almost all my websites because think about it, if you need to change an image, a link, or even a typo, if you are using regular HTML files you have to edit each one individually! This will show you how to save yourself a LOT of time!

First thing you will need is a host, if you do not have one you can get a free account from Host Tornado at http://www.hostt.net.

If you don’t want to get a host online you can always download WAMP for Windows or LAMP for Linux and test everything on your machine first.

Here’s what we’re going to do. We are going to create an index.php file that will pull information from other files that are stored on your server. The end user (person browsing your website) will only see the index.php file. We will specify what page we are looking for by using a little bit of easy php. Ever seen a site who’s website was www.mysite.com/index.php?page=contact? Well that’s pretty much what we’re going to do, with a few twists.

So first thing first, we need to create a new index file. We will call this file index.php because when you go to www.mysite.com it will automatically go to the index.php or index.html file. Since we are creating our website in PHP we will be using index.php.

So next thing to do is to create what i like to call our “includes” files and folder. You will understand this term later on in this tutorial but we use the php function include so that’s why we call it that.

So create a new directory in the same place you have your index.php file and name it anything you would like. I will be using “includes” as my folder name for this example.

Now that we have our new folder we need to create our new includes files. For this example we will be creating a very simple website that will consist of 3 includes files, header.php, footer.php, and contact.php. Create all three of these files under the folder we just created “includes”. All the files will be blank and that’s fine we will come back to them in a few minutes.

Now we need to open up our index.php file and start some coding!

We are going to use the code above first to give you a little background and understand about the “include” function in php. Copy the code from above and paste it into your index.php file.

What this does is when the user goes to www.mysite.com/index.php the index.php file will “include” the header.php file, contact.php file, and footer.php file. So remember how we created our folder “includes” and added all those new files, now it’s time to try it out. Open up any of those files and put some type of HTML inside it and run the index.php file and voila! The web browser says the website is www.mysite.com/index.php but really it’s pulling the information from www.mysite.com/includes/header.php and the two other files.

Now that you have some background with include function we are going to add some code and make it to where we can specify which page we want to see by using a variable. Lets add another file to our “includes” folder and lets call it home.php, this is going to be for the homepage when the user goes to index.php and does not specify any variables.

Now what you will notice in the code above that we have changed is we added the $_GET function, what this does is gets the information that is passed to the index.php file. For instance, since we specified we will be using page you can set a link in your website www.mysite.com/index.php?page=contact, the GET function will pull whatever information you put page equal to. You can change page to whatever you would like, for instance on my site i use just the letter p (www.mysite.com/index.php?p=contact). It’s up to you what you use just make sure you have it correct in your php file.

Now all you need to do is create your website and cut it up into three sections. The header, content, and footer. Since we are using includes we only require one header and footer, you just need to create each “content” file and include it from your php file.

You can even go even further with the code and have www.mysite.com/index.php?page=contact&person=john, this will give you two variables you can use, all you have to do is put the second variable (person) inside the if statement of contact.

In the code above we split up our “contact.php” file into two section, this way we can place the contact information for John right in the middle of our contact.php file. There are many different variations you can do so give it a try, play around with it and love it!

Good luck!

Myles

Orlando, FL

Did this post help you?

Give back and rate it for me!

Related Posts