2013-02-21 88 views
0

我正在尝试为Kohana 3.3项目使用kohana-captcha module。一切工作正常,直到验证。Kohana验证码模块

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

<?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_Alpha Object ([driver:protected] => [response:protected] => MWXF [image:protected] => [image_type:protected] => png) 

有何意见?

+0

我想我的问题是与此相关的https://github.com/kolanos/kohana-captcha/issues/5不过,我不知道如何计算出来。 – Gregory 2013-02-21 20:59:36

回答

1

你应该检查你的POST变量,在这种情况下'验证码',是否等于验证码的实例。因此,您应在if声明之前启动Captcha对象,并在此声明中验证post

事情是这样的:

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

哦,它的工作原理!辉煌!非常感谢你! – Gregory 2013-02-21 23:38:26