0

我正在尝试将kohana-captcha 模块用于 Kohana 3.3 项目。在验证之前一切正常。

问题是无论生成什么图像,验证码模块总是显示不同的答案。这是我的代码示例:

<?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);
    }
}

?>

查看代码:

<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数组也是空的。关键是我看到验证码图像,输入代码,提交表单,然后什么也没收到。Captcha::valid($_POST['captcha'])什么都没有告诉我。当我尝试制作print_r($captcha)Captcha::instance(),它向我展示了一个具有受保护的“响应”属性但它包含完全不同的字母和数字的对象。

例如,我看到一张带有“KX5R”验证码的图像,这是 print_r($captcha) 的结果:

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

有什么建议吗?

4

1 回答 1

1

您应该检查您的POST变量(在本例中为“验证码”)是否等于验证码的实例。因此,您应该在您正在验证的语句之前启动Captcha对象。ifpost

像这样的东西:

$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 
}
于 2013-02-21T23:21:37.750 回答