2

Probably something simple, but heres my problem, I have implememnted a simple captcha into my user system, but I can't seem to use the callback form validation to check to see if the captcha is correct.

I store the answer in the users session, and on form validation I check if it is correct.

You can see it live here: http://77.96.119.180/beer/user/register Please excuse any references to beer ;)

Here is my code:

$this->form_validation->set_rules('captcha', 'Image verification', 'trim|required|xss_clean|integer|callback_captcha_check');

Here is part of the form

// CAPTCHA
$random_word = rand(10000, 99999);
$vals = array(
    'word'   => $random_word,
    'img_path'   => './captcha/',
    'img_url'    => 'http://77.96.119.180/beer/captcha/',
    'font_path'  => './captcha/fonts/pdark.ttf',
    'img_width'  => '150',
    'img_height' => 50,
    'expiration' => 7200
);

$cap = create_captcha($vals);

// Store captcha answer in session
$this->session->set_userdata('captcha_answer', $random_word);

$content .= form_label('Please enter the numbers in the box to verify you are human (Hint: Only numbers will appear)', 'captcha');
$content .= '<div class="left" style="margin-left:20px;">' . $cap['image'] . '</div>';
$content .= form_input(array('name' => 'captcha', 'value' => '', 'class' => 'right', 'style' => 'width:75%; margin-top:5px;'));

And here is the callback function

public function captcha_check($str)
    {
        if ($str == $this->session->userdata('captcha_answer'))
        {
            // Captcha is correct
            $this->form_validation->set_message('captcha', 'The %s was wrong, please try again');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
5
  • if you var_dump($str);exit; in your callback, is it showing your string? Commented Jun 21, 2012 at 22:25
  • nope, I've tried added echo "hello"; and it never appears on the page so the callback never gets called Commented Jun 21, 2012 at 22:28
  • going down the list: your callback function is in the same controller as the one setting the form validation rules and handling the form correct? Commented Jun 21, 2012 at 22:31
  • I would post the entire script but it's a bit long, I can send it to an email and provide line numbers Commented Jun 21, 2012 at 22:35
  • ahhhhh I just noticed your returning false when the captcha matches and true when vice versa Commented Jun 21, 2012 at 22:38

1 Answer 1

2

your validation rules are reversed in your callback function. your return true and return false logic needs to be reversed.

just change your if statement to this:

if ($str != $this->session->userdata('captcha_answer'))
Sign up to request clarification or add additional context in comments.

1 Comment

Haha, I thought it was simple! That works, although now I get the error: Unable to access an error message corresponding to your field name. But I can solve that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.