A previous post requested how CAPTCHA could be added to a node in order to restrict its contents. Unfortunately it is not working for me (perhaps because it is for Drupal 5).

Does anyone know of a simple snippet that would require a valid CAPTCHA before displaying the contents of one specific node?

Comments

soxofaan’s picture

hi,
what you want is not possible out of the box,
but here is some inspiration that should help to do it with some custom coding:
#481408: CAPTCHA before downloading files
#743056: Document how to add a CAPTCHA programmatically

iantresman’s picture

Thanks for the pointers. I figured that there would be some PHP required, after all, the previous post I mentioned, seemed to work for an earlier version of CAPTCHA.

iantresman’s picture

This code snippet will display the contents of a node (page), only if the captcha is successfully validated.

The code snippet seems to be working for me with CAPTCHA Version: 6.x-2.2 and Captcha Riddler 6.x-1.1. I am not a programmer, so do not know whether this code is kosher, conforms to Drupal security policies, or whether it will work in future Captcha upgrades.

Installation:

  • On the CAPTCHA configuration screen at admin/user/captcha, in the Persistence section, select "Omit challenges for a form once the user has successfully responded to a challenge for that form" (otherwise it will just be displayed again).
  • On the CAPTCHA configuration screen, also make sure that the form_id section includes the ID "empty_captcha_form". If it is not added automatically, then you can add it manually on the last row. This matches the ID of the form that we create on this restricted access node (See code below)
  • On the page on which you want to add the captcha, you must change the Input format (on the edit screen) to "PHP code" (otherwise the snippet can not be processed).
  • If you are using a Rich Text Editor (such as CKeditor, WYSIWYG), then you will need to switch to "source HTML" text entry (otherwise the editor will add superfluous markup that will break the PHP code.

Note that:

  • As the administrator, the captcha is displayed with the answer, and I found you need to either (a) log-out (b) use a different Browser to simulate a normal user, in order to see this work properly.
  • Once the captcha is answered correctly (even as the administrator), the captcha is not displayed again. I couldn't figure out how to reset this, so I could carry on testing. I suspect that it is something to do with session IDs, but I don't know how to reset them.
  • There is no error checking on whether the captcha module is actually installed, and an error will be generated if the module is not present.

Code begins:


# Check whether user needs a CAPTCHA

module_load_include('inc', 'captcha');  /* Loads captcha.inc so that the next function is available */ 
$passedCaptcha =  !_captcha_required_for_user($captcha_sid, 'empty_captcha_form');

# Defines an almost empty form just to be able to create a captcha point
# see http://api.drupal.org/api/drupal/developer--topics--forms_api.html/5

if( !function_exists( empty_captcha_form ) ) {
  function empty_captcha_form() {
    $form['submit'] = array('#type' => 'submit', '#value' => t('Validate'));
    return $form;
  }
}

# Display form is user hasn't validated a captcha yet, the real content otherwise
if( !$passedCaptcha ){
  echo "<br><h2>Only accessible if you pass the test</h2>\n";
  echo "Please fill out the form below in order to gain access to the content of this page.<br>\n";
  return drupal_get_form('empty_captcha_form');
} else {

real content of the node!! } /* end of captcha test */ Code ends.
.
Credit due to ingomueller.net for a previous version of this snippet that worked with an earlier version of captcha.

ingomueller.net’s picture

Hi iantresman!

Thanks a lot for your improvements! Finally migrating to drupal 6, I just stumbled about your post. I found that the following was necessary though, as authenticated users did not have access otherwise:

$passedCaptcha = user_access('skip CAPTCHA') || !_captcha_required_for_user($captcha_sid, 'empty_captcha_form');

Cheers, Ingo

Update

What I wrote is actually not true. What is important though is that the persistence type is set to "Omit challenges on a form type once the user successfully responds to a challenge on a form of that type.".

Update2

I do think in the end that my proposed change is necessary. When I was in doubt earlier, I must have had entered a Captcha before, and the form got validated by that...

wundo’s picture

Issue summary: View changes
Status: Active » Closed (outdated)