Skip to content

How to create a page in PHP?

2010 March 15
by Richard Knop

One of the most common questions beginner PHP programmers ask is “How to create a page in PHP?”. What they mean by that is how can they create a website where pages have URIs like these:

http://www.example.com/home

http://www.example.com/about

http://www.example.com/contact

Instead of URIs like these:

http://www.example.com/index.php?page=home

http://www.example.com/index.php?page=about

http://www.example.com/index.php?page=contact

It’s actually a trivial problem but surprisingly people still keep asking it. I guess there are not enough examples around the Web so bellow I will show you probably the most primitive front controller implementation.

What I have described above is called a front controller. To put it differently – a central script accepting all traffic and displaying different content based on GET parameters. Let’s get started!

First, I have created  a simple directory structure for my front controller:

/path/to/root/
    .htaccess
    404.php
    footer.php
    header.php
    index.php
    /path/to/root/pages/
        about.phtml
        contact.phtml
        home.phtml

.htaccess

RewriteEngine on
RewriteRule ^([A-Za-z\-]+)/?$ index.php?page=$1 [L]

What it does is it will redirect you from http://www.example.com/page to http://www.example.com/index.php?page=page silently. This is not necessary but URIs with index.php are ugly and have no semantic meaning.

404.php

<h1>Error 404</h1>

This is just a template for a 404 Not Found page. You can put any HTML you like there.

footer.php

</body>
</html>

All pages will finish with the same HTML written down in the footer.php file.

header.php

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5.  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  6.  <meta http-equiv="Content-Language" content="en-us" />
  7.  
  8.  <title><?php echo $title; ?></title>
  9. </head>
  10.  
  11. <body>

Similarly, all pages share the same header.

Notice the <?php echo $title; ?>. We will set a dynamic title based on a current page in the front controller.

index.php

The most important file. This is the already mentioned front controller:

  1. <?php
  2.  
  3. // first we define the current working directory as a constant
  4. define('BASE_PATH', getcwd());
  5.  
  6. // in case no page is defined, assume the home page
  7. // otherwise, urldecode the page and make it lowercase
  8. $page = (isset($_GET['page']))
  9.         ? strtolower(urldecode($_GET['page']))
  10.         : 'home';
  11.  
  12. // absolute path to a page template
  13. $path = BASE_PATH
  14.         . '/pages/'
  15.         . $page
  16.         . '.phtml';
  17.  
  18. // if a page template exists in the pages directory
  19. if (false === file_exists($path)) {
  20.  
  21.     // send 404 Not Found HTTP status code
  22.     header("HTTP/1.0 404 Not Found");
  23.     // set title
  24.     $title = 'Error 404';
  25.     // set template to be included
  26.     $page = '404.php';
  27.  
  28. } else {
  29.  
  30.     // send 200 OK HTTP status code
  31.     header("HTTP/1.0 200 OK");
  32.     // set title (replace "-" chars with " " and make the first char uppercase)
  33.     $title = ucfirst(str_replace('-', ' ', $page));
  34.     // set template to be included
  35.     $page = $path;
  36.  
  37. }
  38.  
  39. // include the header template
  40. include BASE_PATH . '/header.php';
  41.  
  42. // include the page template
  43. include $page;
  44.  
  45. // and finally, include the footer template
  46. include BASE_PATH . '/footer.php';

I have added some crucial comments above so you ca understand what’s going on there.

about.phtml

<h1>About</h1>

A template for http://www.example.com/about page.

contact.phtml

<h1>Contact</h1>

A template for http://www.example.com/contact page.

home.phtml

<h1>Home</h1>

A template for the home page: http://www.example.com/home or just http://www.example.com/.

Final thoughts

You can add as many new pages as you like by adding new templates to the pages directory. Just remember only alphabetical characters and the “-” character are allowed in their names.

No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS