Posted on 2007-02-02 02:19:58-08 by mccomber
missing something with $captcha->check_code

Hi, I've got things working with the exception of $captcha->check_code. I've got debug code following it that prints $result. $result is null and I get no errors. I've commented out $captcha->check_code and replaced it with a simple if that checks $code against $chars (which I pass back hidden in the form). That works but is a poor hack! Here's my code:

#!/usr/bin/perl -w use strict; use CGI qw/:standard/; use GD::SecurityImage::AC; # Globals my $captcha; # Captcha security image object my $md5sum; # md5 hash sum my $chars; # text of captcha my $flag; # boolean flag: has the form been submitted or not? my $code; # holds users resonse to the captcha my $results; # holds results of captch->check_code function ### Get form parameters ### if (param()) { $code = param('captcha_text'); $chars = param('form_chars'); $flag = param('form_flag'); $md5sum = param('form_md5sum'); } # Run this block only if first run or re-attempt if ($flag ne "submitted") { # create new captcha object $captcha = Authen::Captcha->new( data_folder => '/Users/Doug/Sites/captcha', output_folder => '/Users/Doug/Sites/images', expire => 300, width => 75, height => 50, images_folder => '/Users/Doug/Sites/images', keep_failures => 0, debug => 0, )->gdsi( new => { width => 300, height => 75, lines => 5, bgcolor => '#CCCCCC', font => "/Library/Fonts/Chalkboard.ttf", scramble => 1, angle => 25, ptsize => 20, }, create => [ttf => 'rect', '#000000', '#C8C8C8'], particle => [115, 250], ); # create a captcha. Image filename is "$md5sum.png" my $number_of_characters = 6; ($md5sum,$chars) = $captcha->generate_code($number_of_characters); } ### End $flag based first ###################################### # Print form my $img = "../images/" . $md5sum . ".png"; print header, start_html('Captcha Form'), start_form, img {src=>$img}, hidden('form_md5sum',$md5sum), hidden('form_chars',$chars), hidden('form_flag','submitted'), textfield('captcha_text'), submit, $chars, end_form; # Run this block only if form has been submitted if ($flag eq "submitted") { # check for a valid submitted captcha # $code is the submitted letter combination guess from the user # $md5sum is the submitted md5sum from the user (that we gave them) # $results will be one of: # 1 : passed # 0 : code not checked (file error) # -1 : failed: code expired # -2 : failed: invalid code (not in database) # -3 : failed: invalid code (code does not match crypt) #$results = $captcha->check_code($code,$md5sum); ######### ## debug if ($code eq $chars) { $results = 1; } else { $results = 0; } ######### ## debug if ($results == 1) { print <<ENDHTML; <h1>Success!</h1> ENDHTML } else { print <<ENDHTML; <h1>WTF!</h1> Code: $code<br /> md5s: $md5sum<br /> Resu: $results ENDHTML } } ### End if block

Thanks,
Doug

Direct Responses: 4301 | Write a response
Posted on 2007-02-09 15:55:51-08 by burak in response to 4224
Re: missing something with $captcha->check_code
Hi. You have an error in your code. If you put this after use strict;
BEGIN { use CGI::Carp qw(fatalsToBrowser); }
You can see that your code is die()ing at the line you use check_code(). In your code, $captcha is only defined if $flag ne "submitted". If it has the value "submitted", $captcha will not be defined. Put the $captcha object creation outside of any param check, or create a new one just before calling check_code(). See the SYNOPSIS section of GD::SecurityImage::AC for the usage of the module.
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.