Idea on how to restrain (view) access to a node with CAPTCHAs

ingomueller.net - August 27, 2009 - 20:01
Project:CAPTCHA
Version:5.x-3.2
Component:Miscellaneous
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs work
Description

Hi!

I wanted to restrict the access of a certain node (a page, my contact information) to human visitors. I found a way to do this with CAPTCHA. The content of the node is wrapped into some PHP code that shows an almost empty form before the user has proven his human nature, and the real content otherwise. This form becomes a CAPTCHA point. Maybe someone else can use this idea, or even develop it a bit further. The code can probably be improved, maybe there's a better way to do that, or you guys could integrate this feature to your module. Here's the code:

<?php
# Check whether user needs a CAPTCH
# (he does if he doesn't have the skip CAPTCHA permission or if he hasn't answered a CAPTCHA yet)
$passedCaptcha = user_access('skip CAPTCHA') ||
     
_captcha_persistence_skip('empty_captcha_form') ||
     
$_SESSION['captcha']['success'];

# 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>This page is only accessible for human beings!</h2>\n";
  echo
"Please fill out the form below in order to prove that you're human and to gain access to the real content of this page.<br>\n";
  return
drupal_get_form('empty_captcha_form');
} else {
?>


real content of the node!!

<?php } /* end of captcha test */ ?>

After that, create a CAPTCHA point in the admin menu for the menu id "empty_captcha_form". That should be it!

Comments welcome!

Ingo

#1

bluesband - October 21, 2009 - 08:05

this code not working for my Drupal installation.

subscribing !

 
 

Drupal is a registered trademark of Dries Buytaert.