Analysis

Messageboards can be considered as an extension of the guestbook - indeed, the most simple kind of messageboard you can write is simply a guestbook. Of course, this would be far from ideal, as there would be no way to start a new discussion while leaving the old discussions still happening. Similarly, there would be no way to reply to a particular message - you would just reply to the general mass.

The messageboard system we will be creating will have the following properties:

  • Users can post messages, and the messages will have their HTML tags and bad words ("dog" and "hamster") taken out. These messages can either start a new topic or be a reply to a previous post.

  • Users will be able to edit or delete their messages.

  • Users will be able to create their own messageboards, over which they will have moderator privileges

  • Users will be able to search all the messages for specific terms

These three might seem fairly straightforward at first, but in reality it is quite a lot of work to get that far. Nevertheless, we're going to create a complete system here with quite an advanced amount of functionality. Ideally this will also be tied up with a user system so that users could track their messages and board ownership easily - for this example I will be using simple passwords.

From a front-end point of view, there needs to be a script to read the messageboard index (the list of all available messageboards), a script to read the message index (the list of messages posted to a given board), one to read each message, one to post and reply to messages, one to edit messages, one to delete messages, one to create messageboards, and one to search through the messages. That is eight separate scripts in total, although we could easily combine certain scripts together, such as posting and editing.

While combining scripts will make your admin lower, it also requires much more complicated code and I'd spend more time explaining how one script does two things rather than how the script actually works. As such, each task will be broken down into individual scripts, which we will call index.php, mbindex.php, read.php, post.php, edit.php, delete.php, create.php, and search.php respectively.

Why does joining scripts together make your admin lower? Well, consider a scenario where you are filtering out "dog" and "hamster" from all messages posted to your messageboard. Naturally you do not want people to be able to post an innocuous message, then edit it and fill it with HTML code or obscenities, so you will need to put the same filtering code in your editing script, which means you have code duplication - the same code in two places. Consider what happens if you decide you want to filter "cat" - you need to edit two scripts rather than one, which makes your life harder than it needs to be. Of course, the easiest way is to create a shared function to handle filtering for you!

Thinking from a database angle, we will need just two tables to make our messageboard system work. If you are thinking that two is a bit optimistic, then think harder: what does a messageboard actually need to store? In the ideal scenario, we would have to tables - one for the list of messageboards, and another for the list of messages in those messageboards.

Here is the layout we will be using for the messageboard list table, mblist:

ID

INT

Name

CHAR(255)

Password

CHAR(40)

Filtered

TINYINT

Here is the layout for the matching table to hold the actual messages, mbmsgs:

ID

INT

MBID

INT

Parent

INT

Poster

CHAR(255)

Email

CHAR(255)

Title

CHAR(255)

Message

TEXT

DateSubmitted

INT

Password

CHAR(40)

Note that the password fields are 40 characters long in both tables, a length chosen so that we use the SHA1 hash to store the passwords in a safe format. The "filtered" field in mblist is designed to hold either a 1 or a 0, where a 1 meant that the swear word filtering should be enabled and a 0 meant that messages should be allowed in only stripped of their HTML tags. This is just to give you some ideas for the future - we're not actually going to be implementing this here. Besides, if you wanted to be really cool, you would let people define their own list of words to filter!

Moving on, MBID and Parent in mbmsgs are there to store positioning information for our messages - MBID will store an ID number in the mblist table that is used to identify which messageboard this message is for, and Parent is designed to store a 0 if this is the first post in a thread or any other number for the ID of a post in the mbmsgs table that this message is a reply to.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Development >>

Previous chapter: Creating a messageboard

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.