Project Steve Guttenberg

Using captchas with Project Steve Guttenberg

Posted by Gavin on Wednesday, September 20, 2006 (Link)

I've just committed some new code that allows you to intercept the comment posting process, so that you can implement a challenge-response system like a captcha to avoid comment spam.

In your $conf array you need to define two new keys:

comment_post_validate_controls
this is the name of a function that is called by the printCommentForm() function. It should return a string containing any extra HTML that you might want to insert into the comments form.
comment_post_validate_function
this is the name of a function that is called by the postComment() function. It should return a boolean value that tells postComment() whether or not it should allow the comment to be posted.

Here's how I implemented a captcha system using the new feature and captcha.net:

  1. check out the latest version of PSG from CVS.
  2. go to captchas.net and sign up for an account.
  3. download the CaptchasDotNet.php file in place it in your psg directory.
  4. open the script in which your $conf is located in an editor, and add the new keys like so:
    $config[comment_post_validate_controls] = 'captchaControls';
    $config[comment_post_validate_function] = 'captchaValidate';
  5. in the same file, insert this code:
    	require_once('CaptchasDotNet.php');
    	$CAPTCHA = new CaptchasDotNet(
    			$YOUR_USERNAME,				// client
    			$YOUR_SECRET,				// secret
    			'/tmp/captchasnet-random-strings',	// repository_prefix
    			'3600',					// cleanup_time
    			'abcdefghkmnopqrstuvwxyz',		// alphabet
    			'6',					// letters
    			'240',					// width
    			'80'					// height
    	);
    
    	function captchaControls() {
    		global $CAPTCHA;
    		printf(
    			'<input type="hidden" name="captcha-random" value="%s" />
    			Please enter the letters in the image below into the input:<br /><br />
    			%s<br /><br />
    			<input type="text" name="captcha-text"  /><br /><br />
    			<a href="%s">Click here for a phonetic spelling (MP3)</a><br />
    			<small>powered by <a href="http://captchas.net/">captchas.net</a></small><br /><br />',
    			$CAPTCHA->random(),
    			$CAPTCHA->image(),
    			$CAPTCHA->audio_url()
    		);
    	}
    
    	function captchaValidate($comment) {
    		global $CAPTCHA;
    		return ($CAPTCHA->validate($_POST['captcha-random']) && $CAPTCHA->verify($_POST['captcha-text']));
    	}
  6. The captcha works perfectly!


Comments:

[Avatar]
Re: Using captchas with Project Steve Guttenberg
Posted by Greg (drakos7 (at) gmail dot com) on Friday, January 19, 2007
#2 go to captcha.net and sign up for an account.
Perhaps I am just brain dead today, but I cannot find out where to sign up for the captcha.net account.
[Avatar]
Re: Using captchas with Project Steve Guttenberg
Posted by Greg (drakos7 (at) gmail dot com) on Friday, January 19, 2007
Ahh, it is http://captchas.net (missing 's')
Re: Using captchas with Project Steve Guttenberg
Posted by Alex on Thursday, February 22, 2007
I think your URL for the CAPTCHA service is wrong... it seems you want this instead:

http://captchas.net/registration/
No more comments can be posted to this article.

<< Previous: Project Steve Guttenberg 1.12.0Next: Using Akismet with PSG >>

[ Archives | Search | Syndicate ]
© 2008 ProjectSteveGuttenberg.org.