Commentator: a really simple PHP comments system

Commentator is an extremely simple PHP comments system that uses a MySQL database.

Features

Download

Requires PHP 5+ and a MySQL database.

» commentator.zip (19KB) - changelog

Latest version: 0.7.5 (added 14 March 2011)

If updating from version 0.5, please see this comment.

You can also fork it on Github.

Usage

You need to put some PHP into your own code where you want the comments (and the comment form) to appear. For example, this page contains all comments in a DIV, with an H2 element as a heading, which includes the total number of comments.

<?php
  require_once('../commentator.php');
  $comments = new Commentator('lemurs', 'My page on lemurs');
?>
  <h2 id="comments">Comments (<?php echo $comments->commentcount; ?>)</h2>
  <div class="section">
<?php $comments->write() ?>
  </div>

Spam protection

The script optionally employs the Akismet PHP5 class to avoid spam comments. I really recommend it, as it works really well and spam is pretty ubiquitous. As an example, within two days of putting this page up, there were already comments about viagra being submitted (but they got caught).

The script will also allow you to submit false positives ("Ham") and uncaught Spam to the Akismet service, which helps everyone using it. This is done automatically when you use the "Mark as spam" or "Unmark as spam" buttons (see Managing comments below).

To use it you will need a WordPress API key. It is very easy to get and does not mean you need to get any other WordPress stuff - they'll give it to you for free, which is very nice of them.

When you download the Akismet PHP class, you only need the main PHP file (Akismet.class.php).

Settings

You will need to set some variables at the top of commentator.php. None of these are absolutely critical for the operation of the script, but they are important nevertheless. They are quite self-explanatory:


$akismet_file = 'Akismet.class.php';            // Akismet PHP5 class file
$wpAPIkey = 'xxxxxxxxxxx';                      // WordPress API key
$commentator_from = 'noreply@example.com';      // email address to appear in "From" field
$email_owner = 'me@example.com';                // Your address. Identifies author in comments
$preview = true;                                // allows users to preview comments before submitting
$send_email_on_new_comment = true;              // Sends $email_owner an email when a new comment is posted
$commentator_password = 'password';             // Password to enable you to delete comments
$gravatars = true;                              // Use of gravatars. Set to false to disable, true to use default.
$comments_per_page = 20;                        // Comments to display per page. Set to false to disable pagination
$newest_first = true;                           // Order comments to display newest comments first.
$mark_as_spam_and_delete = true;                // Delete comments automatically when marking as spam

// Allowed HTML tags in comments. Set to '' to disable HTML altogether
$html_comments = '<a><i><b><em><u><s><strong><code><pre><p>';
$htmlfixer_file = 'htmlfixer.class.php';        // HtmlFixer class file to fix bad HTML. Set to false to disable
    

The other gravatar defaults are supported too by setting $gravatars to one of the default gravatar keywords or to the URL for an image of your own.

If using them, the Akismet and HtmlFixer files must be in the same directory as Commentator.

Managing comments

As you can see in the settings section above, you can set a password. This will make a checkbox appear next to each comment, and these buttons at the bottom:

To use this feature, you must log in by clicking the "Admin" link in the bottom right of the comment-posting form. To log out click it again. The old system of putting ?password=xxxx in the address bar will still work, but its use is discouraged as it is very insecure.

Important: Once logged in, reloading the page will log you out automatically unless you have set up a PHP session in your code using session_start. This needs to be done on the page where you inserted the code in the Usage section above before any HTML is output. This is because sessions work with cookies and PHP cannot set a cookie once stuff has been sent back to the browser.

The buttons are pretty self-explanatory. The number in the last button just tells you how many comments are spam. It appears regardless of whether spam comments are showing or not, so use with caution!

The "Unmark as spam" will submit a "Ham" to Akismet, i.e. it will submit a false positive. Please use it sensibly. The same goes for the "Mark as spam", which tells Akismet about uncaught spam. You can set it to delete the comment as well (see settings above).

License

BSD-style license. This means you can pretty much do whatever you want as long as you give due credit.

Contact

If the steps above didn't help, if you encounter bugs or if you have any other questions, email me:

r [at] this domain

Comments (206)

Newest first Oldest first Page 1 of 11 Next 20 »

  1. NADEVALF's avatar NADEVALF

    get gucci aaa and check coupon code available

    #206 – 3 February, 2012 at 12:38 am

  2. RodzKieX Forums Admin's avatar RodzKieX Forums Admin

    Very good Tutorial!,

    -Admin RodzKieX Forums

    RodzKieX Forums

    #205 – 1 February, 2012 at 9:38 pm

  3. quyco's avatar quyco

    Việt Nam

    #204 – 31 January, 2012 at 7:03 pm

  4. df's avatar df

    daf

    #203 – 31 January, 2012 at 3:11 pm

  5. sirleto's avatar sirleto

    i just wanted to test this.

    i expect this comment box here to be exactly what the whole page is about, right?

    :)

    #202 – 31 January, 2012 at 4:22 am

  6. tutntips admin's avatar tutntips admin

    Very good project !

    It inspired me for my website's comment system (tutntips)

    #201 – 31 January, 2012 at 3:54 am

  7. specs's avatar specs

    test, it looks good

    #200 – 30 January, 2012 at 9:31 am

  8. Holly's avatar Holly

    testing

    #199 – 28 January, 2012 at 2:24 pm

  9. Holly's avatar Holly

    testing

    #198 – 28 January, 2012 at 2:24 pm

  10. ime's avatar ime

    neki komentar

    #197 – 26 January, 2012 at 7:09 am

  11. Ankur Datta's avatar Ankur Datta

    Company Mailing Addres

    #196 – 24 January, 2012 at 7:18 am

  12. Kyle's avatar Kyle

    COol story bro needs more vaginas?

    #195 – 23 January, 2012 at 8:51 am

  13. Djotka's avatar Djotka

    hehhehehehhe

    #194 – 19 January, 2012 at 11:06 am

  14. 7u's avatar 7u

    yhjtyju

    #193 – 18 January, 2012 at 1:31 am

  15. chris's avatar chris

    test

    #192 – 17 January, 2012 at 5:14 am

  16. My name here's avatar My name here

    this is a simple test

    #191 – 12 January, 2012 at 2:48 pm

  17. My name here's avatar My name here

    this is a simple test

    #190 – 12 January, 2012 at 2:48 pm

  18. My name here's avatar My name here

    this is a simple test

    #189 – 12 January, 2012 at 2:48 pm

  19. My name here's avatar My name here

    this is a simple test

    #188 – 12 January, 2012 at 2:48 pm

  20. My name here's avatar My name here

    this is a simple test

    #187 – 12 January, 2012 at 2:47 pm

Page 1 of 11 Next 20 »

Post a comment (Optional)
  • Allowed markup: <a> <i> <b> <em> <u> <s> <strong> <code> <pre> <p>
  • All other tags will be stripped, unless they are in a <pre> (use this for blocks of code)
  • External links will have the rel="nofollow" attribute applied