【问题标题】:Kohana Captcha moduleKohana验证码模块
【发布时间】:2013-02-21 20:17:13
【问题描述】:

我正在尝试将 kohana-captcha module 用于 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']) 什么也没告诉我。当我尝试在Captcha::instance() 之后创建print_r($captcha) 时,它向我显示了一个具有受保护的“响应”属性的对象,但它包含完全不同的字母和数字。

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

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

有什么建议吗?

【问题讨论】:

标签: php frameworks kohana captcha


【解决方案1】:

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

类似这样的:

$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 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多