CakePHP 2.2.4 reCAPTCHA验证不起作用

时间:2013-01-08 12:31:18

标签: cakephp recaptcha

您好我有一个注册系统正在使用cakePHP 2.2.4 signup我正在使用此链接Jahdrien/ReCaptcha-Plugin的reCAPTCHA在我的视图中显示,但代码验证无效请尽可能有人告诉我如何进行reCAPTCHA验证?

UsersController.php //我的Cotroller。

<?php
class UsersController extends AppController{
    public $components = array('Recaptcha.Recaptcha');
    public $helpers = array('Recaptcha.Recaptcha');
    public function signup(){

        $d = $this->request->data;
        $d['User']['id'] = null;
        if(!empty($d['User']['password'])){
                $d['User']['password'] = Security::hash($d['User']['password'],null,true);  
        }
        if($this->User->save($d,true,array('username','password','email'))){
            $link = array('controller'=>'Users','action'=>'activate',$this->User->id.'-'.md5($d['User']['password']));
            App::uses('CakeEmail','Network/Email');
            $mail = new CakeEmail();
            $mail->from('noreplay@localhost.com')
                 ->to($d['User']['email'])
                 ->subject('CakePHP Test :: Registration on Ohyeahhh.com')
                 ->emailFormat('html')
                 ->template('signup')
                 ->viewVars(array('username'=>$d['User']['username'],'link'=>$link))
                 ->send();
                 $this->request->data = array();
            $this->Session->setFlash("Your account was successfully created.","notif",array('type'=>'Success'));
        }else{
            $this->Session->setFlash("Please correct the following errors.","notif");
        }

    }
?>

User.php //我的模特。

<?php
class User extends AppModel{
    public $validate = array(
        'username'=>array(
            array(
                'rule'=>'alphaNumeric',
                'allowEmpty'=>false,
                'message'=>'Invalide Username!'
            ),
            array(
                'rule' => array('minLength', '4'),
                'message' => 'Username has to be more than 3 chars'
            ),
            array(
                'rule'=>'isUnique',
                'message'=>'Username already taken!'
            )
        ),
        'password' => array(
                array(
                    'rule' => 'alphaNumeric',
                    'allowEmpty'=>false,
                    'message' => 'Password must be AlphaNumeric!'
                ),
                array(
                    'rule' => array('minLength', '4'),
                    'message' => 'Username has to be more that 3 chars'
                ),
                array(
                    'rule' => array('confirmPassword', 'password'),
                    'message' => 'Passwords do not match'
                )), 
        'confirmpassword' => array(
                            'rule' => 'alphanumeric'
        ),
        'email'=>array(
            array(
                'rule'=>'email',
                'allowEmpty'=>false,
                'required'=>true,
                'message'=>'Invalide mail adress!'
            ),
            array(
                'rule'=>'isUnique',
                'message'=>'Mail adress already taken!'
            )
        )
    );
    function confirmPassword($data)
    {
        if ($data['password'] == Security::hash($this->data['User']['confirmpassword'],null,true)) {
            return true;
        }
            return false;
    }
}
?>

signup.ctp //我的观点。

<?php 

echo $this->Session->flash();
echo $this->Form->create('User');   
echo $this->Form->input('username' ,array('label'=>"Username :")); 
echo $this->Form->input('password' ,array('label'=>"Password :")); 
echo $this->Form->input('confirmpassword' ,array('label'=>"Password (type again to catch typos) :", 'type' => 'password')); 
echo $this->Form->input('email' ,array('label'=>"Email :"));  
echo $this->Recaptcha->show(array(
                'theme' => 'red',
                'lang' => 'en',
            ));
echo $this->Recaptcha->error();
echo $this->Form->end('Register'); 
?>

Recaptcha文件夹位于我的“root / plugin /”文件夹中。

这是ValidationBehavior.php,我不知道如何使验证工作。

<?php
class ValidationBehavior extends ModelBehavior {
    function beforeValidate(&$model) {
        die(' probando funcion de validacion ');
        $model->validate['recaptcha_response_field'] = array(
            'checkRecaptcha' => array(
                'rule' => array('checkRecaptcha', 'recaptcha_challenge_field'),
                'required' => true,
                'message' => 'You did not enter the words correctly. Please try again.',
            ),
        );
    }

    function checkRecaptcha(&$model, $data, $target) {
        App::import('Vendor', 'RecaptchaPlugin.recaptchalib');
        Configure::load('RecaptchaPlugin.key');
        $privatekey = Configure::read('Recaptcha.Private');
        $res = recaptcha_check_answer(
            $privatekey,                            $_SERVER['REMOTE_ADDR'],
            $model->data[$model->alias][$target],   $data['recaptcha_response_field']
        );
        return $res->is_valid;
    }
}
?>

感谢。

2 个答案:

答案 0 :(得分:2)

老实说,试试我们的。

https://github.com/CakeDC/recaptcha

它经过联合测试和记录。你正在使用的那个人实际上做得不好。

答案 1 :(得分:0)

使用CakeDC recaptcha是一个很好的解决方案。刚刚实施它没有问题。如果您收到错误,可能是因为没有加载插件。确保所有文件都放在Plugin \ Recaptcha目录中,并将以下行添加到bootstrap.php中:

CakePlugin ::负载( '的Recaptcha');