2011-02-03 58 views
0

我已經使用了實際cakephp書中的Ascii驗證碼,但我無法同時驗證用戶信息和驗證碼。下面是我的代碼驗證碼登記表

**In user controller ** 

function check() { 
     if (!empty($this->data['User']['secure'])) { 
      if ($this->data['User']['secure'] == $this->Session->read('string')) { 
       $this->Session->setFlash(__('You have entered the right characters', true)); 
      } else { 
       $this->Session->setFlash(__('You have entered the wrong characters. Please, try again.', true)); 
      } 
     } else { 
      $this->Session->setFlash(__('You need to enter the correct characters. Please, try again.', true)); 
     } 
    } 

function register() { 

     $captcha = $this->AsciiCaptcha->getCaptcha(); 

     $string = implode("", array_keys($captcha)); 
     $this->set(compact('captcha', 'string')); 
     $this->Session->write('string', $string); 

     if (!empty($this->data)) { 
      $this->User->create(); 
      if ($this->User->save($this->data)) { 

       $data = $this->User->read(); 
       $this->Auth->login($data); 
       $aro = new Aro(); 
       $parent = $aro->findByAlias($this->User->find('count') > 1 ? 'User' : 'Admin'); 
       $aro->create(); 
       $aro->save(array(
        'model' => 'User', 
        'foreign_key' => $this->User->id, 
        'parent_id' => $parent['Aro']['id'], 
        'alias' => 'User::' . $this->User->id 
       )); 
       $this->Session->setFlash(__('The user has been saved', true)); 
       $this->Auth->logout(); 
       $this->redirect('login'); 
      } else { 
       $this->Session->setFlash(__('The user could not be saved. Please, try again.', true)); 
      } 
     } 

     $groups = $this->User->Group->find('list'); 
     $this->set(compact('groups')); 
    } 

我用許多方法來檢查用戶信息是否正確和代碼匹配或not.But無法找到任何解決辦法。請幫忙。

+0

請記住,會議方式不是一個非常用戶友好的 - http://www.dereuromark.de/2010/08/09/how-to-implement-captchas-properly/ – mark 2011-02-03 15:08:05

回答

0

看起來像你應該在方法的底部有這個,就在/在$組後= ...

$captcha = $this->AsciiCaptcha->getCaptcha(); 
$string = implode("", array_keys($captcha)); 
$this->set(compact('captcha', 'string')); 
$this->Session->write('string', $string); 

你在寫在會話中的東西其在驗證之前使用所以它總是錯的。