简体   繁体   中英

captcha in registration form

I have used Ascii Captcha from practical cakephp book.But I can't validate user information and captcha code simultaneously. 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 = ...

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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