Friday, December 21, 2007

Adding Re-Captcha to php-fusion contact page




This 4 step is to install Re-Captcha into your php-fusion contact form,

  • Download the reCAPTCHA Library,
    extract recaptchalib.php in the directory where you your forms live.
  • If you haven't done so, sign up for an API key.
  • Now we're ready to start modifying your contact.php code.
    • First, add this code to the head of your files:

    • require_once('recaptchalib.php');
      $publickey = "..."; #take it from http://recaptcha.net/api/getkey?app=php
      $privatekey = "..."; #take it from http://recaptcha.net/api/getkey?app=php

      # the response from reCAPTCHA
      $resp = null;
      # the error code from reCAPTCHA, if any
      $error = null;
    • Add this code to validate the CAPTCHA by changing the "sendmessage" condition. Otherwise, the CAPTCHA will appear, but the answers won't be checked. The validation code looks like:
      if (!$error) {
      if ($_POST["recaptcha_response_field"]) {
      $resp = recaptcha_check_answer ($privatekey,
      $_SERVER["REMOTE_ADDR"],
      $_POST["recaptcha_challenge_field"],
      $_POST["recaptcha_response_field"]);

      if ($resp->is_valid)
      {
      require_once INCLUDES."sendmail_include.php";
      sendemail($settings['siteusername'],$settings['siteemail'],
      \$mailname,$email,$subject,$message);
      opentable($locale['400']);
      echo "\n".$locale['440']."\n".$locale['441']."\n";
      closetable();
      }
      else
      {
      opentable($locale['400']);
      echo "\n".$locale['900']."\n";
      closetable();
      }
    • Then add the code to display re-captcha, i prefer to put it before submit html process:
      echo recaptcha_get_html($publickey, $error);
  • Add to locale (/locale/English/contact.php) some words to give a warning about wrong re-captcha or whatever you like :)

    $locale['900'] = "Wrong re-captcha entry!"

  • get full script here, modify it as you want :)
    cheers.

    2 comments:

    1. what about adding this to the news comment section? (version 7)

      ReplyDelete
    2. @Anonymous: sorry. not yet using ver.7 maybe later, for a while, just search google for it :)

      ReplyDelete