PHP Website Structure and Design
My first website was the most confusing piece of code ever. During my time of writing it I learned some usual things and the common used things. I’ll share some of the nice ways to organize your website structure. Those structures may be simple but effective for your own use. Since I am a PHP power user, I’ll make it a PHP based tutorial. In my point of view, having all pages loaded from index is the best for massive websites. That is because you do not share the internal folders with users and everybody uses one single page for viewing, meaning you can password protect everything else on your server.
So here is a basic good directory structure of the website:
/ /include/pages /include/libraries /images
This in my point of view is a really simple organized and safe way to create a website that can have a massive amount of pages and easy indexed. Included folder is where we store all our libraries, layouts, our custom functions, classes and pages. And the images folder speaks for it self. The slash at front means root of the website.
It may look good but I can still make it more better. Even thought people will not be able to access those folders we can make it shorter to save some time ans space, even thought its just few bytes of data. Shorten the “include” to “inc” and “images” to “img” for a start. There is no point having a big word like “libraries” because we can make a mistake in the spelling when writing scripts that deal with files in that folder.
So here is what we have now:
/ /inc/pages /inc/lib /img
Now it looks and feels very simple.
There are 4 major php pages that we need to create first:
- index.php
- the page that loads configuration and all our things in the lib/ folder we made and finally the content page that the user wants to see like home or a list of something - config.php
- place to hold critical configurations like folder names and database user name and password - layout.php
-how our site will look, much better to have a layout in one place rather then writing in every single display page (the beauty of PHP) - home.php
- the default page that is shown if no page was selected
Here is what the structure should look like now:
/ /config.php /index.php /inc/pages /inc/pages/home.php /inc/lib /inc/lib/layout.php /img
Now lets get into more detail of those pages.
config.php
You must have a one place where you can put the critical information. This information is basically passwords and folder names. You can store the folder names in a database but the root things like the password for is best be stored here. This is not required but is best for flexibility.
For instance it is much easier to use $dbpass $dbname $dbuser for entering you main database because if you were to change a database name or pass word all you need to do is edit the config.php where you declared those 3 variables. if you got the pass and the name written in every page then its more of a security risk and you will be in pain updating every page where you connected to the database.
Same goes for folders. Make an array for folder places so then if for some reason you want to change the whole folder or select a new version of files you can just edit this variable instead of updating each page of your site.
Make sure that this page must be included to operate, I talk more about this for the index page. Very important.
index.php
Instead of having lots of pages in the root fo our website like index.php as a home page and gallery.php for the gallery and so on we will have just one single page “index.php”. And instead of writing our home page in it we make this our page server, it listens what page clients wants and it loads them. This means that this page will have the highest load.
Basic code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <php //Set variable that this is the index of the site. define('THEINDEX',TRUE); //Load configuration file. require('config.php'); //Array of website required files. $required_files = array( 'layout.php' //Page layout ); //Include website required files from array or die. foreach($required_files as $file) if (@is_file($di['lib'].$file)) require_once($di['lib'].$file); else die('Required File Not Found: '.$file); //Page request handle. if (@is_file($di['pages'].trim($_GET['page']).'.php')) require_once($di['pages'].trim($_GET['page']).'.php'); else require_once($di['pages'].'home.php'); ?> |
Define some complex variable for security measures. This is how it works, this variable is defines only in this page and it better be something big just in case. Basically every other PHP page on your site at the top of the whole code will have one if statement that checks to see if this was defined. Since this is only defined in the index then pages included by the index will be able to see this variables and pass the if statement true. If the included page at work does not see this variable set then we can simple use die() or redirect the user to the home page. This is just one of the simple ways to prevent code execution, there is also lots of other ways. This is not necessary but works for me.
Require the configuration. (config.php) – Writing the PHP script we will first call the configuration file (config.php) that holds all critical data.
Require the your library scripts. (ex: layout.php) – Then knowing all the folder names that we got from the config.php we can include all of the libraries.
The page selection is done by passing URL variables. So if this is the URL “www.example.com/index.php?page=home” then there is one variable by the name of “page” that has a value of “home” (aside the variables set by the server). Simple right (those who know all this probably falling asleep, but I take newbies into consideration). Choosing what variable name you will use for passing the page name is important because it must be the same for every link that you will write in your website. if you got like 30 pages of dense code then its a pain modifying the variable name. Usually this name will have to be something simple like ‘p’ for page or ‘a’ for action. Lets assume we will use “page” as the variable name to keep things simple. So now its basically things we will pass in the page variable will be just the name of the pages that are located in the /inc/pages folder. So if we want the home.php we will use “home” not the whole thing. The ending is unnecessary. Concluding that first we add .php to the value in the “page” variable and check if that page exist in the /inc/pages folder and if it does then load it. If it does not exist then load our default page “home.php”.
layout.php
This is where we store the HTML header and the fotter of the website. Now this may seem very simple and limited but it is not. This is just a mere start. A lot of modifications can be done.
Basic example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php class Layout { function top() { echo '<html><head><title>Example</title></head><body>'; } function foot() { echo '</body></html>'; } } ?> |
There you have it, a simple page. This means you will not have to create all teh headers and stuff in each page you make, instead you can just use Layout::top(); to show the top part. This is not limited so your your imagination to how far you can modify this and make it more advance.
home.php
Our simple default page that will be shown if no page is selected or page that was selected is not found.
Example code:
1 2 3 4 5 6 | <?php //Cheking for constants defined in index to prevent direct page usage. if (!defined('ININDEX')) die('no peeking'); echo 'Hello World!'; ?> |
The first chunk is what I talked about when creating the index.php. This little piece of code will check to see if that constant was defined. If the constant does not exist it will stop working the rest of script and display “no peeking” as the code says.
You can download all those pages already constructed and at their correct place here. I have also added tiny bit of things to the code and a 1 bar layout.
One example would be to find youth issues discussed as matters of national impor- tance in the front pages of newspapers rather than relegated to the family section. ,
Teachers are those who help us in resolving problems which, without them, we wouldn