简体   繁体   English

注册表中的验证码

[英]captcha in registration form

I have used Ascii Captcha from practical cakephp book.But I can't validate user information and captcha code simultaneously. 我已经从实用的cakephp书中使用过Ascii Captcha。但是我无法同时验证用户信息和验证码。 Below is my code 下面是我的代码

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

I used many way to check whether user information is correct and code is matched or not.But can't found any solution. 我使用了很多方法来检查用户信息是否正确以及代码是否匹配,但是找不到任何解决方案。 Please help. 请帮忙。

looks like you should have this at the bottom of the method, just before/after the $groups = ... 看起来您应该在方法底部,在$ groups = ...之前/之后使用它。

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

you are over writing the stuff in the session before its used in the validation so its always wrong. 在验证之前,您已经在会话中写了很多东西,因此它总是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM