How to create a page in PHP?
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
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-
<head>
-
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
-
<meta http-equiv="Content-Language" content="en-us" />
-
-
<title><?php echo $title; ?></title>
-
</head>
-
-
<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:
-
<?php
-
-
// first we define the current working directory as a constant
-
define('BASE_PATH', getcwd());
-
-
// in case no page is defined, assume the home page
-
// otherwise, urldecode the page and make it lowercase
-
$page = (isset($_GET['page']))
-
? strtolower(urldecode($_GET['page']))
-
: 'home';
-
-
// absolute path to a page template
-
$path = BASE_PATH
-
. '/pages/'
-
. $page
-
. '.phtml';
-
-
// if a page template exists in the pages directory
-
if (false === file_exists($path)) {
-
-
// send 404 Not Found HTTP status code
-
header("HTTP/1.0 404 Not Found");
-
// set title
-
$title = 'Error 404';
-
// set template to be included
-
$page = '404.php';
-
-
} else {
-
-
// send 200 OK HTTP status code
-
header("HTTP/1.0 200 OK");
-
// set title (replace "-" chars with " " and make the first char uppercase)
-
$title = ucfirst(str_replace('-', ' ', $page));
-
// set template to be included
-
$page = $path;
-
-
}
-
-
// include the header template
-
include BASE_PATH . '/header.php';
-
-
// include the page template
-
include $page;
-
-
// and finally, include the footer template
-
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.