Simple CAPTCHA for Greymatter

If you have read any of my previous posts about spam comment entries you will have felt my fustration coming through that a) this happens and b) that there seemed to be little to do about it. However, thanks to a post from Steve Conlan, I seem to have found a simple way of blocking out the spammers.

As the spam comments are made by a machine and not a human the answer is to check for signs of human life. Normally this would be done via entering some sort of distorted word held in an image that cannot be machine read – commonly called a captcha. This was going to be far too difficult for my Perl skills but I found amuch simpler solution elsewhere. This was to ask a simple question that any human (ought) to be able to answer but a machine was going to struggle with. I went for adding the tricky question “What colour are oranges?” to my comment form and so far this seems to have done the trick.

Given that this is simple to add to the existing code I have included details on how to do it below. You may wish to either change the question or even have a number of randomly chosen questions but I found that this works for me.

The changes themselves have to be made in two places: gm-comments.cgi script and the comment form itself.

In gm-comments.cgi you need to add the following subroutine to process the question:

# ————-
# check for spam
# ————-

sub gm_commentspamcheck {

if (lc($IN{‘newcommentspam’}) ne “orange”) {

print “Content-type: text/htmlnn”;



You have not correctly answered the anti-spam question.

Use the Back button on your browser to return to the previous

page and to correct the answer (hint: it is orange).





This subroutine needs to be called immediately before the call to &gm_commentbancheck; as &gm_commentspamcheck;

You can download the complete gm-comments.cgi here, remember to rename it’s extension to .cgi.

In the Comments Templates the standard comments form ({ {entrycommentsform } } Posting Form) need to have the question added as follows:

<hr width=”75%”⁄>
<a name=”comments”><⁄a>
<form action=”⁄gm-comments.cgi#comments” method=post
NAME=”comments” onsubmit=”memory();”>
<p align=”center”>
<font style=”font: bold;”><u>Add A New Comment<⁄u><⁄font>
<input type=”hidden” name=”newcommententrynumber” value=”{{entrynumber}}” ⁄>
<br ⁄>
<br ⁄>
<input type=”text” name=”newcommentauthor” size=”40″ ⁄>

<br ⁄>
What colour are oranges: (anti-spam question)
<br ⁄>
<input type=”text” name=”newcommentspam” size=”40″ ⁄>

<br ⁄>
E-Mail (optional)
<br ⁄>
<input type=”text” name=”newcommentemail” size=”40″ ⁄>
<br ⁄>
Homepage (optional)
<br ⁄>
<input type=”text” name=”newcommenthomepage” size=”40″ ⁄>
<br ⁄>
<br ⁄>
<textarea name=”newcommentbody” cols=”35″ rows=”10″ wrap=”virtual”><⁄textarea>
<br ⁄>
<input type=”submit” name=”gmpostpreview” value=”Preview” ⁄> <input type=”submit” value=”Post It” ⁄><br ⁄>
<input TYPE=”radio” NAME=”cookieme” value=”bake” id=”one” ⁄>Remember
<input TYPE=”radio” NAME=”cookieme” value=”eat” id=”two” ⁄>Forget Me
<script type=”text⁄javascript” language=”JavaScript”>
document.comments.newcommentemail.value = getCookie(“gmcmtmail”);
document.comments.newcommentauthor.value = getCookie(“gmcmtauth”);
document.comments.newcommenthomepage.value = getCookie(“gmcmthome”);

Implement the above and welcome to spam-free comment posting!