hoojamomma

Building a CMS with PHP

PHP is a very powerful web language. You can use it for almost anything. However, one of the most powerful uses for PHP is for making a content mangagement system (CMS). A CMS is very simple to make. However, there are two main ways to make one.

The query-string method

The query string method is probably the simplest method. There is one main template file (usually named index.php). Content is put in seperate files, and a query string is used to insert other files into the content section of the page. A URL for a page would look like this:

http://www.yoursite.com/index.php?page=something

While the URL is kind of ugly, the code is very simple and fast. Here is an example:

index.php:


<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My CMS!</title>
<link rel="stylesheet"
   type="text/css" href="/style.css" />
<meta http-equiv="Content-Type"
   content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Page header</h1>
<div id="nav"><!-- Navigation --></div>
<div id="content"><!--Begin Content -->
<?php
//Check to see if a page is specified.
if (array_key_exists("page", $_REQUEST)) {
   $page = $_REQUEST["page"];
   //Check to see if the file exists.
   if (file_exists($page)) {
      //Insert the page
      require_once($page);
   } else {
      //Insert a 404 message.
      require_once("/404.php");
   }
} else {
   //Include the site index.
   require_once("/main.php");
}
?>
<!-- End Content -->
</div>
<div id="footer"><!-- Footer --></div>
</body>
</html>


Example of a page:

<h1>Included page!</h1>
<p>This is the encluded content.</p>
<?="You can use PHP too!"?>

As you can see, this method is easy to understand and easy to code. Notice the use of the require_once function. This inserts the page, and makes sure that it is only run once. You could also add a @ symbol in front of each require_once function. This would surpress any error messages in your code. This is a good idea for professional sites.

<<Prev< | 1 | 2 | >Next>>