Table of Contents
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 http://dev.splitbrain.org/darcs/mtc
Or download a daily generated snapshot: mtc-latest.tgz
Usage
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:
<html> <head> <title>My Two Cents Test page</title> <?php require_once('mtc.class.php'); $MTC = new MTC(); $MTC->init(); ?> </head> <body> ... <?php $MTC->comments(); ?> <?php $MTC->comment_form(); ?> </body> </html>
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 http://www.chonqued.org) |
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 | &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 |
Administration
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.