John’s Adventures

A PHP Discussion Forum

A screenshot of the discussion forum in actionOkay, a long time ago I had a discussion forum on this site. I took it down because nobody used it and I realised that having the ability to post comments about each article was a better idea. I now have a support forum using bbPress but if you’re interested in creating a light-weight discussion forum yourself, I’ve made the code I originally used available so you can add that capability to your website. You’ll need your web server to support mySQL and PHP (which most do) and you’ll need to know your login details to mySQL. Oh, and it bears more than a passing resemblance to the old Joel on Software discussion forum, which isn’t an accident!

Firstly download the code - DiscussionForum.zip (13KB).

In the zip file you’ll find two files, ‘Discuss.php’ which is the actual php script file that contains all the functionality, and ‘plasma.jpg’ which is a nice graphic to make it look a bit more pretty.

Configuration

Okay, open ‘Discuss.php’ and look at the top few lines:

$g_Title = “An Example Discussion Forum”;
$g_URL = “http://www.mysite.com/”;
$g_ContactEmail = “me@mysite.com”;
$g_DisablePostCountInURLs = “0″;
$g_TopicsPerPage = 30;
$g_MessageListTableName = “MessageList”; // the message list table name in the database
$g_ThreadListTableName = “ThreadList”; // the thread list table name in the database
$g_ThisPage = “Discuss.php”; // the name of this page
$g_HostName = “localhost”; // form mySQL connection
$g_Password = “password”; // to run database script
$g_UserName = “???”; // for mySQL connection
$g_DatabaseName = “???”; // for mySQL connection
$g_MySQLPassword = “???”; // for mySQL connection

You’ll need to change the latter 3 and replace the ??? with your mySQL user name, database name and password respectively. You should be able to get these values from your ISP or from your mySQL configuration tool (if you’re stuck drop me a message using the link at the bottom of this page and I’ll see what I can do).

You’ll also want to give a sensible name to the forum setting the $g_Title variable and make sure that $g_URL, when combined with $g_ThisPage point to the location of the script. For example, as it stands, the script would reside at:

http://www.mysite.com/Discuss.php

Usage

Upload the two files to your server and then browse the ‘Discuss.php’ file through your browser (such as ‘http://www.mysite.com/Discuss.php’). The first time you do this you’ll find that you get a SQL error and the opportunity to run the database scripts and create the tables. Given that you’ve not created the tables yet there’s no way it could work.

Follow the link and enter the password (defaulted to ‘password’, see above) and, if you’ve set the values correctly above you’ll get a success message. Then follow the link to ‘Recent Topics’ and start using the discussion forum. It really is as simple as that.

Management

If you want to delete posts and / or topics, then you simply need to browse to the following location (assuming you posted to www.mysite.com):

http://www.mysite.com/Discuss.php?cmd=manage

Then you put in your password and are greeted by the thread list where you can delete each thread or look at all the posts in a thread and delete specific messages. It’s pretty basic (and you need to watch that you don’t click on an external site as the URL contains your password - hey, it’s only simple). But it works.

Epilogue

If you want to change the layout (as you inevitably will) then have a look through the php file itself and look for the HTML that is included.

I’d just like to mention that this code is based upon the discussion forum once used on Rik Heywood’s site rik.org. He was kind enough to give me a copy of his source when I was first hacking around with php and it helped me greatly. Thanks Rik!

Revision History

Below are the changes that I’ve made to the script over time:

  • Updated the code so it’ll run on PHP5. [22-01-2007]
  • Fixed problem where if you hit ‘Back’ after posting a comment, it would post it again. Fixed bug where if you replied to a topic and then went to the topic list, that topic wouldn’t be marked as read (which it should have been). You can now specify the table names for the discussion forum so you can now host multiple discussion forums in the same mySQL database. [30-12-2005]
  • Added host name to global variable list. Added archiving facility so that if there are more posts than fit on the page, ‘Previous’ and ‘Next’ links will appear at the bottom of the page (about time too) - you can also change the page limit with the variable mentioned above. Improved the robustness of the database access. Made the forum produce XHTML rather than HTML. [20-05-2004]
  • Added e-mail form to send messages to posters without revealing e-mail addresses (just like on Joel’s forum) - thanks to Sam Livingston-Gray. [09-02-2004]
  • Added management functionality to remove and edit posts and topics. [31-07-2003]
  • Initial release. [23-11-2002]

Support

If you find any problems or have any requests, then please: