Creating a website using PHP Includes
Tutorials > Creating a website using PHP Includes
This is pretty basic stuff but for those who are not familiar with PHP includes and are finding it hard to start using PHP to build a website, this might help you on your way. I have had a few requests to write a simple tutorial on how to build a website using PHP includes. This is written as a way to build a website, maintain it and still have individual pages for good search engines ranking positions.
The method we are going to use as the example, will allow you to have as many pages are you require in your website from just a couple of pages or a couple of hundred. Using this method only the centre section on each page will be different (the page content). The top, side and bottom pages are all 'included' automatically by PHP.
For instance, if you have a website of about 20 pages and you need to change the menu system or add a few more links, then by making use of PHP includes you can change just the one file (the included one) and then all others pages which utilise this included file will also be automatically updated. Just think of the time saved by changing the one file compared to changing 20 of them! From a designer's point of view, it does not bear thinking about!
Back to what we were doing, the layout of our page will be like:-
Step One A good rule is to keep all files which are related to a particular website in the same folder. This prevents any dead links or linking errors which may creep in later on when the website gets quite large. This applies to all websites you create and not just this tutorial. Another good tip which is worth remembering is to keep all filenames you create for pages in lowercase.
Now we know to keep all our files in the same folder, create a folder and call it whatever you want, just remember where it is. Then open up your HTML editor and create a new page, save this page as template.php inside the folder you have just created. The extension of the page you create has to end with .php and not .html. All page extensions need to end with .php to allow PHP to parse the pages and include the includes! Type the following into the blank page in source view or if you know a little HTML, create the tables in WYSIWYG view.
Using the code above, we have created a table with two columns. We will use the first column as our navigation menu.
Step Two Now we have the actual page which will create our individual pages from, what we need now are the pages to include. So open up a new blank page in your editor and then save this blank page and call it header.php
Repeat this process for footer.php and menu.php. So now you should have 3 blank pages and the template.php page. You now have all the pages created that will form the basis of your website. Remember this is a tutorial only and you can add any colour format / style after you have got to know a bit about how to use the PHP includes in a website.
Pages you should have created are:-
- template.php (Our main page which we will be using again later)
- header.php (Our included page header)
- footer.php (Our included footer page)
- menu.php (Our included page which will have our menu links on)
Step Three Open up the header.php page and add some text or logo. For our template just add some text which says: "Welcome to PHP Include Test".
Open up the footer.php page and add a copyright notice. The footer can be used for other purposes including more links, but all this can be added later. We will just use it for our copyright notice.
Open up the menu.php page and add the following:
Now we are happy with how our site is progressing, it is time to open up the template.php page. You will notice this page is blank and it will remain so. We are just going to use this page as a template page to create our other pages. So with this page open in your HTML editor, save this page as index.php.
Open this new page (index.php) as this is our new main index page. Repeat this process as many times as you want creating a number of pages to correspond with your website.
Now we have the foundation and pages created, add the text to all the pages you have just created and then open up the menu.php page again. You now have the number of links and pages that you need to add to this file.
For instance, if you created 3 pages from the template.php page (index.php, about.php, contact.php) you can now add these links to the page. Anything you now add to the pages header.php, menu.php footer.php will automatically appear on every page on your website if you use the template,.php page to create extra pages.
Now, to view the pages you have created you will need to upload them to a PHP enabled web server. Nearly all hosts support this now as standard. Point your browser to the uploaded index page and you should be able to see the basic layout and pages we have just created. Have a p[lay around adding text and images so you get the full idea of what PHP Includes can do. It's a great way to create and make a website very maintainable.
After following this tutorial, you should be able to create your own website which is very easy to maintain and consists of loads of pages with quality content for the search engines to find.