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
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.
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.
Now, create a directory called 'admin'. This will contain all of the files for the CMS administration.
In the admin directory, create these files.
That's it for this part of the series. Be sure to subscribe to the RSS feed to receive updates on this series.