2013-01-08 86 views
0

你好我有一個在cakePHP中工作的註冊系統2.2.4 signup我從這個鏈接使用的reCAPTCHA Jahdrien/ReCaptcha-Plugin顯示在我的視圖中,但驗證的代碼不工作請能有人告訴我如何進行reCAPTCHA驗證?CakePHP 2.2.4 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('[email protected]') 
       ->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 // My Model。

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

誠實地嘗試我們的。

https://github.com/CakeDC/recaptcha

它領導的統一測試和記錄。你使用的那個實際上並沒有做得很好。

+0

你好,謝謝你的答案我在你給我的鏈接中使用了reCAPTCHA,現在recaptcha顯示了,但是在驗證它時我收到以下錯誤:「錯誤:調用成員函數verify() -object文件:/home/sites/androtutorials.com/public_html/app/Controller/UsersController.php 行:7「我可能錯過了這一行」將要使用recaptcha的控制器需要包含Recaptcha組件。「我不知道如何包含組件。請任何想法請關於這個?謝謝 – user1818439

+0

burzum,我遵循你的教程,但現在看起來像RecaptchaComponent.php不工作。我得到這個錯誤致命錯誤 錯誤:調用成員函數attach()在非對象上 File:/.../Controller/Component/RecaptchaComponent.php Line:120 – gerl

+0

嘿,我正在使用CakeDC recaptcha,我只是有一個小問題:有沒有一種方法可以在模型中進行驗證,而不是控制器,如https://github.com/CakeDC/recaptcha/blob/master/Docs/中所述Documentation/Setup.md? – lucasnadalutti

0

使用CakeDC的recaptcha是一個很好的解決方案。剛剛實施它沒有問題。如果出現錯誤,可能是因爲該插件未被加載。確保所有文件都放在Plugin \ Recaptcha目錄中,並將以下行添加到bootstrap.php中:

CakePlugin :: load('Recaptcha');