0

您好,我有一个在 cakePHP 2.2.4 中工作的注册系统,从这个链接中注册我正在使用的 reCAPTCHA Jahdrien/ReCaptcha-Plugin 在我的视图中显示,但代码验证不起作用请有人告诉我该怎么做reCAPTCHA 验证?

UsersController.php // 我的控制器。

<?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;
    }
}
?>

谢谢。

4

2 回答 2

2

老实说,试试我们的。

https://github.com/CakeDC/recaptcha

它经过统一测试和记录。您正在使用的那个实际上做得并不好。

于 2013-01-09T02:09:50.087 回答
0

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

CakePlugin::load('Recaptcha');

于 2013-10-02T19:07:53.327 回答