2012-09-22 38 views
1

我使用的代码验证码:https://github.com/solutudo/cakephp-captcha图形验证码不是在登录表单工作在CakePHP

这是我的代码user_controller.php在登录操作:

public function login() { 
     if (!empty($this->data) 
      && !empty($this->Auth->data['User']['username']) 
      && !empty($this->Auth->data['User']['password'])) { 
      // captcha code 
      if ($this->RequestHandler->isPost()) { 
       $this->User->setCaptcha($this->Captcha->getCode()); 
       $this->User->set($this->data); 
       if ($this->User->validates()) { 
        // captcha code 
        $user = $this->User->find('first', array(
         'conditions' => array(
          'User.email' => $this->Auth->data['User']['username'], 
          'User.password' => $this->Auth->data['User']['password']), 
         'recursive' => -1 
         )); 
        if (!empty($user) && $this->Auth->login($user)) { 
         if ($this->Auth->autoRedirect) {    
          $this->redirect($this->Auth->redirect()); 
         } 
        } else { 
         $this->Session->setFlash($this->Auth->loginError, $this->Auth->flashElement, array(), 'auth'); 
        } 
       } 
      } 
     } 
    } 

查看user.ctp

<?php 
    echo $this->Form->create(array('action'=>'login')); 
    echo $this->Form->input('username'); 
    echo $this->Form->input('password'); 
    // captcha code 
    echo $this->Html->image('captcha.jpg', array('style' => 'padding: 0.5%;')); 
    echo $this->Form->input('captcha'); 
    // captcha code 
    echo $this->Form->end('Login'); 
    ?> 

型号user.php的

<?php 
    class User extends AppModel { 
     public $actsAs = array(
      'Captcha' => array(
       'field' => 'captcha', 
       'error' => 'Captcha code entered invalid' 
      ) 
     ); 
    } 
    ?> 

验证码验证和验证码不工作,没有输入验证码我能够登录,我已经使用相同的代码添加功能与相同的控制器user_controller.php,并且时间captcha代码工作。

我希望用户在输入正确的验证码后登录。

+0

随着调试,你看到任何错误?你有附件吗?模型上的验证字段?你认为如何产生它?我们需要更多信息来帮助你。 – jeremyharris

+0

不,我没有发现调试模式的任何错误,是的,我已附加captcha component.i更新与视图和模型代码的问题。 –

+0

验证码比较代码是否正在运行?在那里放一些调试语句。查看提交的验证码和正在比较的验证码的值。他们的价值观是你期望的吗?如果不是,为什么?按照这些值的路径找到问题的发生地点。这是基本的调试。 –

回答

0

通过阅读代码,您似乎必须在尝试验证之前生成验证码。我在代码中看不到您生成验证码的地方。也许你有一条路线指向/img/captcha.jpg来生成函数(就像在文档中一样),但我在这里没有看到。我也没有看到任何你打电话给CaptchaComponent::generate()的地方,它实际上在会话中保存了验证码。

确保以下文件在您的routes.php文件中。

Router::connect('/img/captcha.jpg', array('action' => 'captcha')); 

,并把下面的函数在AppController.php文件:

// auto-outputs an image 
public function captcha() { 
    $this->autoRender = false; 
    $this->Captcha->generate(); 
} 

基本上,当你的图像创建它实际上航线此函数创建代码,并在会话保存它。用户点击提交,并且您的登录功能尝试验证。如果无效,则会呈现视图,并替换为新的验证码。