May 07

As you may have read before, I've been having a real hard time with spambots picking up and exploiting this site's open comment system. It was getting ridiculous to the tune of over 900 spam comments per week.

 

I finally had enough and decided to put in my "Ghetto Captcha" on the site. 

It was actually very easy to implement, and I'm going to post the code here in hopes of helping other BlogEngine.NET users combat spammers.

Basically what you need to do is change the CommentView.ascx (control) design-view code to bypass the default post validation and insert an intermediate javascript validation.

 

Step 1: Add the new text field:

<input type="checkbox" id="cbNotify" style="width: auto" tabindex="7" />
<label for="cbNotify" style="width:auto;float:none;display:inline"><%=Resources.labels.notifyOnNewComments %></label><br /><br />
What is the name of this blog? <input type="text" id="captchaTest" />  <=== ADD THIS
<input type="button" id="btnSaveAjax" value="<%=Resources.labels.saveComment %>" onclick="return preTest()" tabindex="7" />    <== change onclick
<asp:HiddenField runat="server" ID="hfCaptcha" />

 

 

Step 2: Add the new preTest() javascript

function preTest()
{
    if(document.getElementById("captchaTest").value == "futurepr0n")
    {
        return BlogEngine.validateAndSubmitCommentForm();
    }
    return false;
}

Step 3: There is no step 3

 

SO that's it! Spammers be gone, and very little code change.

 

I realize it's hard to follow code like this out of context, so I've attached the source code for the CommentView.ascx that we're using on this site. Hopefully you can use it to your advantage!

 

CommentView.ascx (6.85 kb)

Tags: | |