验证码以注册形式

时间:2011-02-03 07:24:42

标签: php cakephp

我从实用的cakephp书中使用过Ascii Captcha。但我无法同时验证用户信息和验证码。以下是我的代码

**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'));
    }

我用很多方法来检查用户信息是否正确以及代码是否匹配。但是找不到任何解决方案。请帮忙。

1 个答案:

答案 0 :(得分:0)

看起来你应该在方法的底部,在$ groups = ...

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

你在会议中使用的东西在用于验证之前,所以它总是错误的。