HOWTO: Add reCAPTCHA support to Discuz! registration form

To prepare for this, you'll need:

  1. Basic understanding to PHP programming language.
  2. A reCAPTCHA API public-private key pair valid to your site. If you don't, please go to the below site to sign up for one: https://www.google.com/recaptcha/admin/create
  3. Some understanding to your own site's template settings.

OK. Let's start.

  1. First, you must download the latest reCaptcha PHP library from.
    Please go to this site: Downloads - reCaptcha
    (At the time I'm writing this guide, the version is recaptcha-php-1.11)

  2. Decompress the zip file, and you'd have a folder looks like recaptcha-php-X.XX.

  3. Use FTP to access your site files. In the folder that contains your Discuz! fourm, there is a folder called include. upload the folder recaptcha-php-X.XX to that folder.

  4. Now. You must edit the file register.php of your forum. You may download it to your machine first, then upload it back after edit.

    Open the file, find these line:
    1.         if($seccodecheck) {
    2.                 $seccode = random(6, 1);
    3.         }
    4.         if($secqaa['status'][1]) {
    5.                 $seccode = random(1, 1) * 1000000 + substr($seccode, -6);
    6.         }
    7.  
    8.         include template('register');
    9.  
    10. } else {
    At the line before "include template('register');", plaste these code:
    1.         // recaptcha hack start
    2.         require_once dirname(__FILE__) . "/include/recaptcha-php-X.XX/recaptchalib.php";
    3.         $recaptcha_publickey = 'replace this with your real public key';
    4.         $recaptcha_html = recaptcha_get_html($recaptcha_publickey);
    5.         // recaptcha hack end


    Remember to change the variable $recaptcha_publickey to your own reCAPTCHA public key.



  5. One more change to this file.

    Find these lines:
    1.         require_once DISCUZ_ROOT.'./include/discuzcode.func.php';
    2.  
    3.         $email = trim($email);
    4.         $username = addslashes(trim(stripslashes($username)));
    5.         $alipay = trim($alipay);
    6.  
    7.         if(strlen($username) < 3) {
    8.                 showmessage('profile_username_tooshort');
    9.         }
    10.         if(strlen($username) > 15) {
    11.                 showmessage('profile_username_toolong');
    12.         }
    13.  
    14.         if($password != $password2) {
    15.                 showmessage('profile_passwd_notmatch');
    16.         }
    Below the line "require_once ...", insert these code:
    1.         // recaptcha hack start
    2.         require_once dirname(__FILE__) . "/include/recaptcha-php-X.XX/recaptchalib.php";
    3.         $recaptcha_privatekey = 'replace this with your real private key';
    4.         $resp = recaptcha_check_answer ($recaptcha_privatekey,
    5.                   $_SERVER["REMOTE_ADDR"],
    6.                   $_POST["recaptcha_challenge_field"],
    7.                   $_POST["recaptcha_response_field"]);
    8.         if (!$resp->is_valid) {
    9.           // What happens when the CAPTCHA was entered incorrectly
    10.           showmessage('reCAPTCHA failed. reCAPTCHA said: ' . trim($resp->error));
    11.         }
    12.         // recaptcha hack end
    Again, remember to change the variable $recaptcha_privatekey to your own reCAPTCHA private key.

  6. Save the file register.php and put it back.
  7. Now the PHP part is done. We still need to change to template file so users can see the reCAPTCHA image. Please find the template file register.htm.

    The actual location depends on your template settings. Note that if you let users to determine their own template setting, you'd probably need to change the register.htm.

    If you sticked to the default settings, the folder-name of your template files should be default. In this case, the template file your need to change is templates/default/register.htm



  8. Open register.htm. Find these line:
    1.                         <th><label for="email">{lang email} *</label></td>
    2.                         <td>
    3.                                 <input type="text" name="email" size="25" id="email" onBlur="checkemail()" tabindex="6" />
    4.                                 <span id="checkemail">{lang register_email_recommend}<!--{if $regverify == 1}-->&nbsp; {lang register_email_comment}<!--{/if}-->
    5.                                 <!--{if $accessemail}-->&nbsp; {lang register_email_invalid}<!--{elseif $censoremail}-->&nbsp; {lang register_email_censor}<!--{/if}--></span>
    6.                         </td>
    7.                 </tr>
    8.  
    9.                 <!--{if $regstatus > 1}-->
    10.                         <tr>
    11.                                 <th><label for="invitecode">{lang invite_code}<!--{if $regstatus == 2}--> *<!--{/if}--></label></th>
    12.                                 <td><input type="text" name="invitecode" size="25" maxlength="16" value="$invitecode" id="invitecode" onBlur="checkinvitecode()" tabindex="7" />
    13.                                 <span id="checkinvitecode"></span>
    14.                         </td>
    And in between the <tr> and </tr>, insert these lines:
    1. <!-- reCaptcha hack start -->
    2. <!--{if $recaptcha_html}-->
    3.                 <tr>
    4.                   <th><label for="recaptcha">reCaptcha *</label></th>
    5.                   <td>$recaptcha_html</td>
    6.                 </tr>
    7. <!--{/if}-->
    8. <!-- reCaptcha hack end -->
    The result should looks like this:
    1.                 <tr>
    2.                         <th><label for="email">{lang email} *</label></td>
    3.                         <td>
    4.                                 <input type="text" name="email" size="25" id="email" onBlur="checkemail()" tabindex="6" />
    5.                                 <span id="checkemail">{lang register_email_recommend}<!--{if $regverify == 1}-->&nbsp; {lang register_email_comment}<!--{/if}-->
    6.                                 <!--{if $accessemail}-->&nbsp; {lang register_email_invalid}<!--{elseif $censoremail}-->&nbsp; {lang register_email_censor}<!--{/if}--></span>
    7.                         </td>
    8.                 </tr>
    9.  
    10.  
    11. <!-- reCaptcha hack start -->
    12. <!--{if $recaptcha_html}-->
    13.                 <tr>
    14.                   <th><label for="recaptcha">reCaptcha *</label></th>
    15.                   <td>$recaptcha_html</td>
    16.                 </tr>
    17. <!--{/if}-->
    18. <!-- reCaptcha hack end -->
    19.  
    20.                 <!--{if $regstatus > 1}-->
    21.                         <tr>
    22.                                 <th><label for="invitecode">{lang invite_code}<!--{if $regstatus == 2}--> *<!--{/if}--></label></th>
    23.                                 <td><input type="text" name="invitecode" size="25" maxlength="16" value="$invitecode" id="invitecode" onBlur="checkinvitecode()" tabindex="7" />
    24.                                 <span id="checkinvitecode"></span>
    25.                         </td>
  9. Save this file back to the server. Everything should be working just fine.

P.S. This has been tested on Discuz 6.0 and should be OK on other versions.