0
votes

I'm trying to use kohana-captcha module for a Kohana 3.3 project. Everything works fine until the validation.

The problem is that Captcha module always shows different answer no matter what image has been generated. Here is the example of my code:

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_User extends Controller_Template {

    public $template = "template";

    public function action_create()
    {
            if (isset($_POST['captcha']))
            {
                print $_POST['captcha'];
                print ">>>".Captcha::valid($_POST['captcha'])."<<<";
            }

            $captcha = Captcha::instance();

            $this->template->content = View::factory('user/create')
                ->bind('captcha', $captcha);
    }
}

?>

View code:

<form method="post" action="/user/create/" class="form-horizontal" id="form">
    <div class="control-group">
        <label class="control-label" for="inputCaptcha">
            <?=$captcha?>
        </label>
        <div class="controls">
          <input type="text" id="inputCaptcha" placeholder="Код с картинки" name="captcha">
          <span class="help-inline"></span>
        </div>
    </div>    
</form>

$_SESSION and $_COOKIE arrays are also empty. The point is that I see the captcha image, enter the code, submit a form and then receive nothing. Captcha::valid($_POST['captcha']) shows me nothing. When I try to make print_r($captcha) after Captcha::instance() it shows me an object with a protected "response" property but it contains absolutely different letters and digits.

For example, I see an image with a "KX5R" captcha code, here is the result of print_r($captcha):

Captcha_Alpha Object ( [driver:protected] => [response:protected] => MWXF [image:protected] => [image_type:protected] => png )

Any advices?

1
I assume my issue is related to this one github.com/kolanos/kohana-captcha/issues/5 However, I don't know how to figure it out. - Gregory

1 Answers

1
votes

You should check if your POST variable, in this case 'captcha', is equal to the instance of the Captcha. So you should initiate Captcha object before your if statement where you are validating post.

Something like this:

$captcha = Captcha::instance();

$this->template->content = View::factory('user/create')
    ->set('captcha', $captcha);

if ($this->request->method() === Request::POST)
{
    if (Captcha::valid($_POST['captcha']))
        .. do something if captcha is OK
    else 
        ..do something if captcha is not OK 
}