Creating a PHP CMS – Part 1

So far, my experience with PHP has been doing things like making small adjustments to WordPress plugins. So, I decided that it was time that I actually learn to use PHP. I thought that a good way to do this is by creating a simple CMS.

In this series of posts, I will go over the steps to create a simple CMS using PHP and MySQL. I will try to explain everything, but if there is anything you don't understand, w3schools has a pretty good PHP reference, or you can leave a comment and I will do my best to help you out.

I will be adding a new post in this series every Monday, Wednesday and Friday.

To create a CMS you will need

  • A server with PHP and MySQL installed
  • Something for viewing MySQL databases and executing queries, such as phpMyAdmin

Getting Started

The Database

First of all, you will need to create a MySQL database to store information for your CMS. Once you've created it, you need to add a user to the database.

After you've created the database and added a user to it, run the following SQL queries. If you're using phpMyAdmin, enter them under the SQL tab of your database.

CREATE TABLE pages (
    id INTEGER AUTO_INCREMENT,
    title VARCHAR(150),
    body TEXT,
    DATE VARCHAR(10),
    PRIMARY KEY (id)
);

This creates a table for storing the pages for the CMS. 'id' is a number that uniquely identifies each post. The AUTO_INCREMENT part means that each time you insert a record into the table, this number is one more than the last one. 'title' contains the title of the post. VARCHAR(150) means that it can hold a string up to 150 characters long. 'body' contains the main text for the page.

'date' stores the time that the post was written. It is 10 characters long because we will be storing the date in Unix time. Since it can only store 10 digits, it won't be able to store dates after the year 2038 problem. If anyone knows a way to fix that, please leave a comment to let me know how.

PRIMARY KEY (id) means that 'id' will be a unique identifier.

CREATE TABLE settings (
    name VARCHAR(20),
    VALUE VARCHAR(20)
);

This creates a table for storing settings. It can hold a setting name, and a value for the setting. Each one can be up to 20 characters long. If you plan on expanding on the CMS later, you might want to set the values higher.

The Files

After we've setup the database, we need to create some files. First, create a directory on your server that will contain these files.

Create the following files, and leave them empty for now.

  • index.php
  • functions.php
  • config.php

Now, create a directory called 'admin'. This will contain all of the files for the CMS administration.

In the admin directory, create these files.

  • index.php
  • new.php
  • create.php

That's it for this part of the series. Be sure to subscribe to the RSS feed to receive updates on this series.

Jump to a Post in the Series

  1. Part 2
  2. Part 3
  3. Part 4
  4. Part 5
  5. Part 6
  6. Part 7

Stay Updated

Did you enjoy this post? Don't miss a single post by getting free updates!

35 Comments

  1. August 11, 2009

    Hi, I hope so that this series will help me finally to understanding PHP & MYSQL.

    When we can expect second part?

  2. August 11, 2009

    Kabarovsky: Part 2 will be posted tomorrow, and a new post in this series will be published every Monday, Wednesday and Friday.

  3. August 11, 2009

    Ok, tnx a lot!

  4. August 12, 2009

    You posted a nice post. This will really help to understand the PHP CMS development.

  5. August 12, 2009

    Thanks for the comments, everyone!

  6. August 12, 2009

    I know create database, tables etc. in phpMyadmin but where should go this code:

    CREATE TABLE pages (
    id INTEGER AUTO_INCREMENT,
    title VARCHAR(150),
    body TEXT,
    date VARCHAR(10),
    PRIMARY KEY (id)
    );

    ..in one file for example database.sql or what?

  7. August 12, 2009

    Kabarovsky: If you're using phpMyAdmin, go to your database, and then under the SQL tab, enter the code into the box and click go. I will update the post to include this.

  8. August 12, 2009

    Tnx! I figure out everything in these two posts.. Really useful

  9. August 18, 2009

    The way to solve the year 2038 problem is to make 'date' the type of 'DATEIME', someting like 2009-08-18 21:23, and you can use the php function strtotime() to change the DATETIME type to UNIXTIME type to use for the function date().
    That's all to your problem.
    My English is poor, because my native language is Chinese.

  10. August 18, 2009

    Misspelling the word 'DATETIME', sorry about that.

  11. August 18, 2009

    Hua Chen: Thanks! I'll be sure to remember that.

  12. August 18, 2009

    Eric Bannatyne: You are welcome!

  13. January 14, 2010

    This is awesome! I can see where you're going with this - which is an effective yet simple full functional CMS for individuals. I am going to complete this entire series and create a site with it to see the full functionality of it.

    I have been wanting to write a custom CMS for about a year, as I do many small projects for non-profits.

    • January 14, 2010

      I hope you do. Building your own CMS can be a great learning experience (it was for me), and it's also a nice feeling to have something working that you put together yourself.

      If you create a functional site with this, I'd love to see it when it's complete!

  14. January 5, 2011

    Absolutely great Eric. I recently started using wordpress, and my PHP experience has also been limited to editing the pages there within. I too, figured it was about time to branch out and actually learn PHP. My research led me to your site and this post. It has been helpful so far, and I look forward to finishing a functional site. Thanks for all of your time and effort.

  15. January 15, 2011

    Nice! you made creating a CMS easy and I will give it a try myself. My php experience is mostly minor changes in WordPress and a few other small programs a friend wrote for me.

  16. January 20, 2011

    Thanks for such a concise and informative post, Eric. You make it all sound so easy. I have yet to master web designing so I'm grateful for informative and helpful blogs such as yours.

  17. May 23, 2011

    hi, I am read your article and download the file in part 7, but can not start your cms in local. I think if you put SQL code and complete cms (with files and sql codes) in a zip file and we download it, it is better effect on my mind!

    thank you

  18. November 22, 2011

    A very informative post regarding creating a PHP CMS. very helpful post and informative for new developers like me.

  19. December 15, 2011

    thanks a lot for giving very useful information

  20. December 26, 2011

    'date' stores the time that the post was written. It is 10 characters long because we will be storing the date in Unix time. Since it can only store 10 digits, it won't be able to store dates after the year 2038 problem. If anyone knows a way to fix that, please leave a comment to let me know how.

    • February 9, 2012

      This creates a table for storing settings. It can hold a setting name, and a value for the setting. Each one can be up to 20 characters long. If you plan on expanding on the CMS later, you might want to set the values higher.

Trackbacks/Pingbacks