Hi

Drupal version: 5.3
Captcha module version: 5.x-3.1

The current version of Captcha doesnot work with Page cache enabled. This could be a big disadvantage in a large site with many pages. To resolve the same and cache the page content we could apply the below patch.

common.inc

function drupal_page_footer() {
+ //To enable page cache
+ global $user;
+ if(arg(0) == 'node' && is_numeric(arg(1)) && !$user->uid ){
+ $GLOBALS['conf']['cache'] = TRUE; //Cache this page
+ }
if (variable_get('cache', 0)) {
page_set_cache();
}
module_invoke_all('exit');
}

image_captcha.module

function image_captcha_image($seed=NULL) {
+ global $user;
if (!$seed) {
return;
}
// get the code to draw from $_SESSION
$code = $_SESSION['image_captcha'][$seed];
+ if(is_null($code) && $user->uid == 0){
+ $newseed = mt_rand();
+ +// regenerate a CAPTCHA code
+ $allowed_chars = _image_captcha_utf8_split(variable_get('image_captcha_image_allowed_chars', IMAGE_CAPTCHA_ALLOWED_CHARACTERS));
+ $code_length = (int)variable_get('image_captcha_code_length', 5);
+ $code = '';
+ for ($i = 0; $i < $code_length; $i++) {
+ $code .= $allowed_chars[array_rand($allowed_chars)];
+ }
+ $_SESSION['captcha'] = array();//to avoid "cookie enable error"
+ $_SESSION['image_captcha'][$newseed] = $code;
+ }

// unset the code from $_SESSION to prevent rerendering the CAPTCHA
+ if($user->uid !=0 ) {
unset($_SESSION['image_captcha'][$seed]);
+ }
// only generate an image if there is an code

We could furthur enhance and work towards removing session for anonymous user entirely and cache the captcha along with the page cache, refersh it only when used. Any suggestions.

Thanks

Comments

soxofaan’s picture

Title: Captcha doesnot work with Page cache » CAPTCHA and page caching
Version: 5.x-3.1 » 6.x-2.x-dev
Category: support » feature
Status: Active » Postponed (maintainer needs more info)

The current version of Captcha doesnot work with Page cache enabled.

CAPTCHA does work with page caching enabled: it just disables page caching on the pages where it adds a CAPTCHA.
This is a feature, otherwise the challenge would be cached, and you would have the same challenge for every visitor, which breaks CAPTCHA.

Another thing: CAPTCHA 5.x-3.1 is not the current version of CAPTCHA: for Drupal 5 there is already a 5.x-3.2 version (jan 2009).
Also: Drupal 5.3 is insanely outdated: there is already a version 5.20! I would recommend to upgrade your setup urgently.

Moreover, CAPTCHA 5.x-3.x is not really actively maintained anymore: bug fixes are handled, but feature requests go in CAPTCHA HEAD (which is 6x.-2.x now). Note that 6.x-2.x already got rid of most of the SESSION usage you mentioned.
Your issue looks like a feature request (albeit a bit unclear for the moment), so this should be handled in 6.x-2.x.

If you post patches, it is better to attach them as a file, instead of copy-pasting them in the body of your post.
Also, you seem to hack common.inc, which is a big no-go in Drupal. You should only change files in the CAPTCHA module.

wundo’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)