User Tools

Site Tools


My Two Cents

My Two Cents is a simple PHP class to add a comment function to any PHP script, similar to the well known PHP Webnotes script (used in the PHP manuals). PHP Webnotes wasn't updated for several years and has some security problems. This class is much simpler but should be enough for most users.

Getting it

You can check out a copy using darcs:

$> darcs get

Or download a daily generated snapshot: mtc-latest.tgz


You need to create a table in a MySQL database using the provided db.sql script, first.

The class file needs to be included and an object has to be created. This object then can be used to display available comments and a comment form. The following shows the simplest possible usage:

<title>My Two Cents Test page</title>
  $MTC = new MTC();
  <?php $MTC->comments(); ?>
  <?php $MTC->comment_form(); ?>

The class provides the following functions:

Function Description
init([page]) Initializes the class, handles POSTs and prints CSS. Needs to be called in the <head> section
comments() Displays available comments
comment_form() Displays the form to add new comments
comment_count([page]) Return the number of comments for the page (page param is optional)

To group comments belonging to one page My Two Cents uses the $page parameter given in the init function. If you leave the parameter empty the current URL path (PHP_SELF) is used. You may want to specify this your self eg. using a pageid from your CMS.

Advanced usage

The behaviour of My Two Cents can be configured through some class properties. You need to set them before calling the init() call.

Property Default Description
db_host localhost MySQL database host
db_user mtc User for the MySQL database
db_pass mtc Password for the MySQL database
db_name mtc Name of the MySQL database
self mtc.class.php The URL to the mtc.class.php file on your Webserver (needed for CAPTCHA image)
blacklist blacklist.txt A list of regexps to check against spam (See
captcha true Set to false to disable the CAPTCHA security image
audio false Set to true to enable audible CAPTCHAs (requires captcha option)
addcss true init() usually prints some CSS, if you don't want this set this property to false
target Set to '_blank' to open URLs in a new window
adminpass A password needed to delete a comment
notify E-Mail to be notified on a new comment
gravopts &amp;rating=R Give additional options for the Gravatar URL, @MD5@ in the string will be replaced by the md5 of the email address
lang english strings This array holds all language strings used


An administrator can delete comments by enabling the admin mode. To do so add the parameter MTC[admin]=1 to the URL. Each comment will now show a delete button and field for entering the admin password. Just enter the admin password as set through the adminpass property (see above) and press the button to delete a comment.

sw/my_two_cents.txt · Last modified: 2009/07/24 10:43 by