2012-09-22 37 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(); 
} 

基本上,當你的圖像創建它實際上航線此函數創建代碼,並在會話保存它。用戶點擊提交,並且您的登錄功能嘗試驗證。如果無效,則會呈現視圖,並替換爲新的驗證碼。