HOWTO: Add reCAPTCHA support to Discuz! registration form
July 21, 2010
To prepare for this, you'll need:
- Basic understanding to PHP programming language.
- 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
- Some understanding to your own site's template settings.
OK. Let's start.
- 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) - Decompress the zip file, and you'd have a folder looks like recaptcha-php-X.XX.
- 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.
- 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:At the line before "include template('register');", plaste these code:- if($seccodecheck) {
- $seccode = random(6, 1);
- }
- if($secqaa['status'][1]) {
- $seccode = random(1, 1) * 1000000 + substr($seccode, -6);
- }
- include template('register');
- } else {
- // recaptcha hack start
- require_once dirname(__FILE__) . "/include/recaptcha-php-X.XX/recaptchalib.php";
- $recaptcha_publickey = 'replace this with your real public key';
- $recaptcha_html = recaptcha_get_html($recaptcha_publickey);
- // recaptcha hack end
Remember to change the variable $recaptcha_publickey to your own reCAPTCHA public key. - One more change to this file.
Find these lines:Below the line "require_once ...", insert these code:- require_once DISCUZ_ROOT.'./include/discuzcode.func.php';
- $email = trim($email);
- $username = addslashes(trim(stripslashes($username)));
- $alipay = trim($alipay);
- if(strlen($username) < 3) {
- showmessage('profile_username_tooshort');
- }
- if(strlen($username) > 15) {
- showmessage('profile_username_toolong');
- }
- if($password != $password2) {
- showmessage('profile_passwd_notmatch');
- }
Again, remember to change the variable $recaptcha_privatekey to your own reCAPTCHA private key.- // recaptcha hack start
- require_once dirname(__FILE__) . "/include/recaptcha-php-X.XX/recaptchalib.php";
- $recaptcha_privatekey = 'replace this with your real private key';
- $resp = recaptcha_check_answer ($recaptcha_privatekey,
- $_SERVER["REMOTE_ADDR"],
- $_POST["recaptcha_challenge_field"],
- $_POST["recaptcha_response_field"]);
- if (!$resp->is_valid) {
- // What happens when the CAPTCHA was entered incorrectly
- showmessage('reCAPTCHA failed. reCAPTCHA said: ' . trim($resp->error));
- }
- // recaptcha hack end
- Save the file register.php and put it back.
- 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 - Open register.htm. Find these line:
And in between the <tr> and </tr>, insert these lines:
- <th><label for="email">{lang email} *</label></td>
- <td>
- <input type="text" name="email" size="25" id="email" onBlur="checkemail()" tabindex="6" />
- <span id="checkemail">{lang register_email_recommend}<!--{if $regverify == 1}--> {lang register_email_comment}<!--{/if}-->
- <!--{if $accessemail}--> {lang register_email_invalid}<!--{elseif $censoremail}--> {lang register_email_censor}<!--{/if}--></span>
- </td>
- </tr>
- <!--{if $regstatus > 1}-->
- <tr>
- <th><label for="invitecode">{lang invite_code}<!--{if $regstatus == 2}--> *<!--{/if}--></label></th>
- <td><input type="text" name="invitecode" size="25" maxlength="16" value="$invitecode" id="invitecode" onBlur="checkinvitecode()" tabindex="7" />
- <span id="checkinvitecode"></span>
- </td>
The result should looks like this:- <!-- reCaptcha hack start -->
- <!--{if $recaptcha_html}-->
- <tr>
- <th><label for="recaptcha">reCaptcha *</label></th>
- <td>$recaptcha_html</td>
- </tr>
- <!--{/if}-->
- <!-- reCaptcha hack end -->
- <tr>
- <th><label for="email">{lang email} *</label></td>
- <td>
- <input type="text" name="email" size="25" id="email" onBlur="checkemail()" tabindex="6" />
- <span id="checkemail">{lang register_email_recommend}<!--{if $regverify == 1}--> {lang register_email_comment}<!--{/if}-->
- <!--{if $accessemail}--> {lang register_email_invalid}<!--{elseif $censoremail}--> {lang register_email_censor}<!--{/if}--></span>
- </td>
- </tr>
- <!-- reCaptcha hack start -->
- <!--{if $recaptcha_html}-->
- <tr>
- <th><label for="recaptcha">reCaptcha *</label></th>
- <td>$recaptcha_html</td>
- </tr>
- <!--{/if}-->
- <!-- reCaptcha hack end -->
- <!--{if $regstatus > 1}-->
- <tr>
- <th><label for="invitecode">{lang invite_code}<!--{if $regstatus == 2}--> *<!--{/if}--></label></th>
- <td><input type="text" name="invitecode" size="25" maxlength="16" value="$invitecode" id="invitecode" onBlur="checkinvitecode()" tabindex="7" />
- <span id="checkinvitecode"></span>
- </td>
- 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.