Just another web developer - Not!

more information

previous page

Simple CMS

Simple Content Managed Site

What we are going to build here is a very simple Content Managed site. The code has been limited to the bare necessities because the purpose is to provide direction rather than a solution.


These instructions have been written using WampServer which runs under the Windows operating system. WampServer is for Windows. There are versions for Linux (Lamp) and Mac (Mamp).

WampServer is a Windows web development environment. It allows you to create web applications with Apache, PHP and the MySQL database. It also comes with PHPMyAdmin to easily manage your databases. Download WampServer

By default, Apache listens to port 80. This is a problem if windows iis is also loaded and using that port. The solution that I use is to change Apache.conf to listen to port 8080. Then my phpMyAdmin url looks like http://localhost:8080/phpmyadmin/. To achieve this find any http.conf files in folders under wamp. Edit them to change the line Listen 80 to Listen 8080.

If you use Skype, there can also be a conflict with port 80. If you have this problem, open Skype, Go to Tools->Options->Advanced Settings Tab->Connection and Uncheck the �Use Port 80 and 443 as alternatives for incoming connections�

Once installed it provides access to an Apache server, MySql database including phpMyAdmin which is an administration tool and PHP. That is all we need to get our site working.

The folders for our files

After installation of WampServer you will have a folder called wamp under which, amongst others, will be a folder called www. This is where our site will be stored. We are looking to create a directory structure like this:

  • wamp
    • www
      • site01
        • documentation
          • maketestdb.php
        • class
          • dbconnect.php
          • test_page.php
        • css
          • site.css
        • phpscripts
          • config.inc.php
        • index.php

PHP Classes - A very brief introduction

It is not my intention to provide a tutorial on object oriented programming (oop). The benefits to be gained from oop are realised when we do more than just move functions into classes. I will leave that discussion to others.

This is not really correct but it may help some. When I say method think function. When I say property think variable.

To explain what is going on here, we need to understand how to create an object from our class and then call its methods and obtain its property values. I will use test_page as our class name as it matches the example. Here goes:

Before we can do anything with a class we need to tell php where to find it using include or require (even better require_once). Because I also want to check if the class already exists and I don't want to have the full path to the class scattered all over my code, I have a function in config.inc.php called includeClass that does the necessary checking and knows how to find the class. Therefore, to include the code is simply a matter of calling includeClass('test_page');

Next we need to create an instance (object) out of our class (instantiate the cass to creata an object). Our class is called test_page. We know that because it starts out with 'class test_page' and then wraps everything else in curly braces. So to create an instance we use $tpo = new test_page();. (I have used $tpo but it could be any properly formed variable name) From now on when we want to access our class methods or properties we use $tpo-> followed by the method or property followed by the arguments required in brackets..

Just one more thing that I want to explain is the use of $this-> in a class. When we are coding a class and we want to refer to another method or property in the same class we do not need to include the code or instantiate the class we merely use $this->.

The Files

Here we describe the files in each folder and their purpose. The code is well populated with comments.


maketestdb is a sql script that will:

  • Create the database
  • Create a user
  • Grant permissions to the user
  • Create a table
  • Populate the table


There are two classes in this folder.

dbconnect.php provides the database connectivity. This is a simplified version which just provides the functionality required. There are many samples of more thorough database access classes available and well explained on the web. The version provided here is aimed at providing the functionality needed for this project rather than a full data access class.

test_page.php is the class that serves the site page content and builds the navigation menu. The methods are quite thoroughly populated with comments. Let's look at what it does.

  • We start of by declaring the name of the class
  • Next we declare some variables to hold the navigation menu and the page content. (private properties) These are declared as private so that they can not be accessed from outside the class.
  • Next we set up the functions that will allow us to access these properties in a controlled way from outside the class. There is a get and a set for each one that we want to make available.
  • Then we have our public methods as follows:
    • setup_page: sets the navigation menu and the requested page content properties
    • edit_page: Sets the navigation menu and places the edit content form into the content property
    • update_page: updates the content in the database from the edit page form.
    • login_user: Places the login form in the page content property
    • check_login: checks the username and password and sets the session variable loggedin to y
    • logout: sets the session variable loggedin to n. Effectively logging out.
  • Once the navigation menu and page content have been set up, the calling program can use $otp->get_menu() to get back the navigation menu list or $otp->get_content() to get back the page content
  • The private methods are:
    • update_pagedb: instantiates the dbconnect class and updates the table with the new content from the form. Before we can update the database with the input we need to clean it up. Magic quotes used to be used to do this but is now depricated. If it is not turned on, we need to use mysql_real_escape_string on the content before putting it into the database. Then we determine if the update was successful or not and set up a notification.
    • navigation_menu: reads the database and sets the menu property to contain an unordered list of menu items. Then we add either Edit this page and logout or login depending on the logged in status of the visitor.
    • page_content: reads the database to get the page content requested and places it into the content property.
    • edit_form: will only be available if the visitor is logged in and provides the form to update the content for the selected page in the table
    • login_form: provides a form to enable the user to log in.
    • check_loginDetails: checks the login username and password and sets the session variable loggedin to y on success or n on failure


site.css contains the cascading styles for the site. This is not a css exercise and so the code here is minimal.


Config.inc.php contains some site wide definitions. At present these are only the database connection variables and a function that knows how to include the class files and make sure that they are only included once. If you expand on the site, it is likely that you will want to add other variables to this list. It is useful to have them all conveniently located in the one place.

site root

index.php is the file that serves the required content. The header and footer sections are ready for code that will be presented on every page. The navigation menu is set to just a plain unordered list and is ready for styling. Perhaps even some javascript. Content is provided from the database based on the page being requested.

  • We need to have session_start() at the top of our index page so that it can use session variables.
  • Work out which page we are going to load. It would be better to check if the page requested actually exists in the database
  • Once we have decided what page to use we include the class and instantiate it.
  • Now we need to understand what is going on and we use the GET and POST variables to determine this:
    • Take action
      • if(isset($_POST['btnLogin'])) checks if we are coming from the login form. If so then we check the username and password to see if they are ok.
      • if(isset($_GET['logout'])) checks if our visitor has clicked on the logout link. If so then we set the loggedin session variable to n.
      • if(isset($_POST['btnPageUpdate'])) checks if our visitor has updated the content. If so then we update the database with the new content.
    • Set up navigation menu and content
      • if(isset($_GET['login'])) checks if the login link has been selected. If so the navigation menu is set up and the login form is placed into page content.
      • if(isset($_GET['edit'])) checks if the Edit this page link has been selected. If so the navigation menu is set up and the content edit form is placed into page content populated with the existing content and ready for changes.
      • Otherwise: the navigation menu is set up and the content from the database is placed into page content.
  • The head section is bog standard except that it is missing the meta tags. See 'Where to from here' below.
  • Because we have already called the required method of our class, the menu and content properties are available to us whenever we need them. We put their values on the page with the echo $otp->get_menu(); and echo $otp->get_content(); statements

Where to from here

  • Add a sequence column to the table and change the select statement to order by sequence. Then you can control the order in which the menu items are presented.
  • Update the login. Ideally, these would use a saparate authentication class.
  • The table containing page content can be expanded to include items such as: Page Title, Meta keys, Meta Description.
  • Add an Authorised column to the table so that you can add pages only available to authorised visitors.
  • If the Page paremeter in the url is an issue to you, Look at mod rewrite in your favorite serach engine.

This is by no means a fully functional cms. However, it may well help to get you started. It has been written to provide a working example of a process that will work and is ready for expansion.

Why not to do this

After preparing this example, I had a look on the web for php + Simple CMS. There are several other examples. It was unpleasant to see some of the responses to the one that I read thoroughly.

There were two comments that I enjoyed and I have placed these at the foot of this page with a link

Let me try to avoid such responses.

  • This is not intended to be used on the web. It is written to demonstrate how, with a small anmount of code, one can create a working cms.
  • I have ignored some security aspects of the process for the sake of brevity. When implementing a site, that is going to be available to the public, it is important to understand the security necessary for that site and implement it.
  • If, on the other hand, one is learning how to start using php and MySQL, it is useful to get to the stage of a working example quickly. Then the exploration can proceed.

Specific to this example

  • The login process adopted here was implemented to be small and part of the test class. This exercise is not about authentication and authorisation.
  • It is assumed that the logged in user will not be attempting to destroy the system and so no attempt has been made to protect the data entry form.

Basically, here is a small application that will provide a limited degree of functionality. Some may find the process exciting. As I did when I first started working with php and MySQL.

Responses to flames

Here, the author was attempting a very similar exercise. building your first simple cms. Many of the comments made appear to have been written to support large egos rather than for any constructive purpose.

Why not use a framework

Comment: Fur fuck sake, use a framework! I agree that any PHP developer should be able to do this in 5/10 minutes, but come on, why waste time like this? This is why PHP applications grow to be unmaintainable monstrocities of vague undocumented code�

Response: I know, JEEZ, why would you want to LEARN something when it�s already done. That�s why I�d never learn to play an instrument, there are already loads of albums already there for me to listen to.

signed: Chris Coyier (author)

The final response

Now� No one who works with open source technologies should dare bash another for freely sharing knowledge. Ever. Correct mistakes out of respect for the readers, but don�t bash the author. He�s merely trying to contribute to the community. That�s what open source is all about.

signed: The Smart One

Constructive criticism will be gratefully received and reflected here. Feedback will be moderated.

Download Now

Download the zipped site files

How To Use

  1. Install WampServer or similar
  2. Download the site files and unzip them to a folder under www. (C:\wamp\www\site01)
  3. Open phpMyAdmin (localhost/phpmyadmin/)
  4. Select Import and browse to www/site01/documentation/maketestdb.sql and then select Go
  5. Open the site (localhost/site01/)


Please Enter the Blue security code before submitting


  • Image Gallery
  • Date specific content
  • Up to 50 Polls
  • Self registration
  • Content Authorisations
  • Dynamic menus
  • Contact forms
  • Blog on any page
  • Product showcase

Catching up

Copyright © Tony Hutson 2012

Powered by Guybon.com. Affordable web sites that are easy to maintain