Posted January 16, 2010 by Spyros in PHP Programming

PHP Design Patterns – How to Properly Create a Website Index Page


If you are a newcomer to PHP programming but an experienced programmer overall, chances are that you find PHP pretty simple to program with. PHP is actually a combination of C++ and Perl, created so that we can write our websites along with database management and more.

In PHP, there is a very important difference between writing code and writing good code. Of course, this is true for every programming language, let alone PHP. Knowing how to properly design your website’s index page is the first and important step towards creating a solid website.

The Amateur Way to Write Your Index Page

The typical way to write your index page, if you are not really that knowledgeable in PHP, is just go about coding whatever there is for the index page. For instance, suppose that our index page has a login form and some introductory text. Our approach would be to just write the code for the index page and then, for every link on that page, create another page. This would mean that we now have an index.php page, a contact.php page, a mail.php page and more.

Don’t feel bad if you do it this way. I’ve done it myself and every new PHP programmer has done so as well. However, it’s time to learn of a more efficient way to code in PHP.

The “index.php?page=home” Way

Have you noticed that many websites follow this pattern ? Instead of having a home.php page, you see something like “index.php?page=home”. The same happens for every page of the website. What happens is that the index.php page includes the code of the other webpages, as asked. The variable page that you notice in the url is actually a typical $_GET type global variable that you can read in order to identify what is the page that the browser asks for. Here is the actual code of a typical index page :

$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 'home';

    case 'home':           break;
    case 'mail':           break;
    case 'contact':        break;
        $page = 'home';


This is a very basic example as you see. The index page just gets the $_REQUEST[‘page’] global variable ($_REQUEST is a union of $_GET and $_POST, retrieves whatever is available). Now the switch starts. But why have a switch and not just include the php page ? Well, this is actually a security measure against file inclusion attacks. It is always good to handle unexpected input. If an attacker somehow manages to upload a new php page, say hello.php, this index page, without the switch structure, would execute it normally when instructed with index.php?page=’hello’. However, if the switch is there, hello.php never gets executed. Instead, the default home.php gets executed. This is always a very good practice and you should really stick to it. Of course, there is much more in protecting PHP code, but this is a small addition as well.

You could now go about creating your own template for writing websites. Remember that it is very important that you know of the essential PHP requirements in order to write much more efficiently than usual. Also, if you are not familiar with it, pay special attention to Smarty and learn to use it to create your templates.