IMPLEMENTING CAPTCHA ON THE WEBSITE MEMBERSHIP PLUGIN 1.04+ SIGNUP FORM - Aug 18th, 2011


NEW FOR MEMBERSHIP 1.04+

I wanted to use CAPTCHA validation on my membership signup form and although RE-CAPTCHA was pretty easy to implement, it
was a pretty intrusive addition to many web pages.

I discovered another alternative in a program called Captcha Creator. http://www.captchacreator.com/ They’re based in
Romania and their support is a bit spottier then I’d like. (I’ve grown accustomed to the exceptional support offered
by Interactive Tools) But now that I’ve been able to implement their program, I can format the Captcha display to fit
my page design and it works like a charm. Again, their program is not free, and an unlimited use license costs $50.

Implementing the “out of the box” version of Captcha Creator on my membership signup form was throwing a bunch of
“session already started” errors. The programmers at Captcha Creator revised the way the code was implemented on my
page and also made some changes in the captchac_lib.php file and a few others.

*** After you’ve uploaded the “captcha” directory to your server, download the revised files from
http://www.thecmsbcookbook.com/downloads/captcha_creator.zip and replace the ones in your “captcha” directory with
those.

On my existing membership signup form I changed CAPTCHA CREATOR’s Installation Step 2 code to better suit my design.

Instead of:



<table cellpadding=5 cellspacing=0 bgcolor="#E4F8E4">
<tr bgcolor="#AAD6AA">
<td colspan="2"><font color="#FFFFFF" face="Verdana" size="2"><b>Image Verification</b></font></td>
</tr>
<tr>
<td style="padding: 2px;" width="10"><img src="/captcha/captchac_code.php" id="captcha"></td>
<td valign="top"><font color="#000000">Please enter the text from the image</font> &nbsp; <br /> <input type="text"
name="Turing" value="" maxlength="100" size="10">
[ <a href="#" onclick=" document.getElementById('captcha').src = document.getElementById('captcha').src + '?' + (new
Date()).getMilliseconds()">Refresh Image</a> ] [ <a href="/captcha/whatisturing.html"
onClick="window.open('/captcha/whatisturing.html','_blank','width=400, height=300, left=' + (screen.width-450) + ',
top=100');return false;">What's This?</a> ]
</td>
</tr>
</table>



I used:



<table>
<tr>
<td valign="top"><span class="body-text-bold">CAPTCHA Image Verification</span><br /><br />
<span class="body-text">To help reduce spam, please type the characters in the image into the blank box before you
click on submit.<br />
</span> <br />
<a href="/captcha/whatisturing.html" onclick="window.open('/captcha/whatisturing.html','_blank','width=300,
height=300, left=' + (screen.width-450) + ', top=100');return false;">What is CAPTCHA?</a></td>
<td>
<table cellpadding=5 cellspacing=0 >
<tr>
<td style="padding: 5px;" width="300" height="100"><img src="/captcha/captchac_code.php" id="captcha"><br />
<a href="#" onclick=" document.getElementById('captcha').src = document.getElementById('captcha').src + '?' + (new
Date()).getMilliseconds()">Click to change the displayed characters</a><br /></td>
</tr>
<tr>
<td style="padding: 5px;"><input type="text" name="Turing" value="" maxlength="100" size="20" />
<br />
<br /> </td>
</tr>
</table></td></tr>
</table>


To make this all work in Membership 1.04+, at the top of the viewer I called the CAPTCHA program and checked for errors
with:



<?php
include_once('captcha/captchac_lib.php');
session_write_close ();

if (@
$_REQUEST['submit']) {
$errorsAndAlerts = "";
if (!@
$_REQUEST['Turing']) {$errorsAndAlerts .= "To help prevent spam, you must enter characters in the image into
the empty box below before submitting.<br /> <br /> \n";}
else
{
$Turing_code = $_REQUEST["Turing"];
if (
CheckCaptcha($Turing_code) !=1 ) { $errorsAndAlerts .= "The characters you entered do not match those
displayed. Please try again.<br /> <br /> \n";}

}
}
?>


And in the error checking code below I removed the clear errorsAndAlerts code line:


$errorsAndAlerts = "";

So that it only appeared once at the top of the viewer code. (Otherwise it will clear the captcha error messages
__


Don't forget to use "save" or "submit" consistently throughout your viewer code. I think you can use either one but I've
settled on submit.
__


*** LEGACY FOR MEMBERSHIP 1.03 AND EARLIER



In the error checking section of my page after:



//error checking
$errorsAndAlerts = "";

if (!@$_REQUEST['username']) { $errorsAndAlerts .= "You must choose a username!<br />\n"; }



I added this code:



if (!@$_REQUEST['Turing']) {$errorsAndAlerts .= "You must enter the CAPTCHA code!<br />\n";}
else
{
$Turing_code = $_REQUEST["Turing"];
if ( CheckCaptcha($Turing_code) !=1 ) { $errorsAndAlerts .= "The CAPTCHA code you entered does not match. Please try
again.<br />\n";}
}



For CAPTCHA CREATOR’s Installation Step 3 I replaced this:



<?php
include('captcha/captchac_lib.php');
$Turing_code = $_REQUEST["Turing"];
if (
CheckCaptcha($Turing_code) !=1 )
{
echo
"<b><font color=red>The Captcha Code you entered is invalid. Please press the Back button of your browser
and try again</font></b>";
return
1;
}
?>



With this:



<?php
include_once('captcha/captchac_lib.php');
session_write_close ();
?>


So the complete code at the top of my viewer, above the error checking section looks like this:



<?php
include_once('captcha/captchac_lib.php');
session_write_close ();
?>
<?php
require_once "cmsAdmin/lib/viewer_functions.php";
?>
<?php if (!@$GLOBALS['WEBSITE_MEMBERSHIP_PLUGIN']) { die("You must activate the Website Membership plugin before you
can access this page."); } ?>


***(FOR V2.06 AND ABOVE SEE BELOW)***
_

NOTE: If you’ve updated to V2.06 you’ve probably discovered that the error checking at the top of the lib/init.php
file looks different, and pages using Captcha Creator are throwing errors again.

Well, thanks to CMSB User Djulia, the fix is in.

He discovered that when the function session_write_close (); is correctly written into the getRecords code at the top pf
your page, the errors disappear, without the need to modify the lib/init.php file.

Calling the Captcha program first, then closing the session, then calling the CMSB viewer_function is the implementation
that worked for me:



<?php
include_once('captcha/captchac_lib.php');
session_write_close ();
?>
<?php

require_once "cmsAdmin/lib/viewer_functions.php";

list(
$common_informationRecords, $common_informationMetaData) = getRecords(array(
'tableName' => 'common_information',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$common_informationRecord = @$common_informationRecords[0]; // get first record

?>
<?php include_once "/path_to_your/spambot-email-protector.php" ?>


END LEGACY SECTION



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service