2015-09-07 25 views
0

我試圖將google的reCaptcha添加到我的某個頁面。我一直在關注本教程https://codeforgeek.com/2014/12/google-recaptcha-tutorial/並完成了安裝和表單網站上的所有工作。然而,我堅持在PHP方面。我已經將數據存儲在數據庫中,然後發送電子郵件。我一直在嘗試各種方法將reCaptcha整合到現有的功能中,但沒有運氣。將reCaptcha代碼添加到php腳本(CakePhp)

public function calc() { 

    $this->set('title_for_layout', 'Fun Tests'); 
    $this->set('meta_description', 'Check out these funny tests'); 


    if(!$this->Session->check('isLoggedin')) 
    { 
     $this->Session->write('Temp.redirect_url', Router::url(null, true)); 
    } 
    if(!isset($this->params['named']['u'])) { 
     $this->Session->setFlash(__(WRONG_ACCESS_MSG), 'default', array('class' => ERROR_CLASS)); 
     $this->redirect(array('controller' => 'pages', 'action' => 'home')); 
    } 
    //echo $this->Session->read('Temp.redirect_url'); 
    //get the link detail 
    $this->loadModel('User'); 
    $actual_user_id = $this->params['named']['u'] - Configure::read('Site.prefix_to_add_in_userid'); 
    //echo $actual_user_id; 
    $sender_array = $this->User->find('first', array('conditions' => array('User.id' => $actual_user_id))); 
    if(empty($sender_array)) { 
     $this->Session->setFlash(__(WRONG_ACCESS_MSG), 'default', array('class' => ERROR_CLASS)); 
     $this->redirect(array('controller' => 'pages', 'action' => 'home'));   
    } 
    $this->set(compact('sender_array')); 

    if(!empty($this->request->data)) { 
     $this->{$this->pm}->set($this->request->data); 
     if ($this->{$this->pm}->validates()) { 
      $this->{$this->pm}->create();    
      $this->request->data['Result']['user_id'] = $this->Session->read('Auth.User.id'); 

      if ($this->{$this->pm}->save($this->request->data, true)) { 
       $this->Session->setFlash(ADD_MSG, 'default', array('class' => SUCCESS_CLASS)); 
       //send email to the User who created this link if he has the get_notifications flag set to 1 
       if($sender_array['User']['get_notifications'] == 1) { 
        $Email = new CakeEmail(); 
        $Email->to($sender_array['User']['email']); 
        $Email->subject(__('New Results For Your Test', Configure::read('Site.title'))); 
        $Email->viewVars(array('data' => $sender_array['User'])); 
        $Email->template('Tested_successfully', 'default'); 
        $email_pars = array('email' => $Email); 
        $this->centralEmail($email_pars); 
       }    
       $this->render('result_saved'); 
       //$this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(ERROR_MSG, 'default', array('class' => ERROR_CLASS)); 
      } 
     } else { 
      // didn't validate logic 
      $errors = $this->{$this->pm}->validationErrors; 
      $this->Session->setFlash(ERROR_VALIDATE_MSG, 'default', array('class' => ERROR_CLASS)); 
     } 
    } 
    $this->set('title_for_layout', 'Fun Tests'); 
    $this->set('meta_description', 'Check out these funny tests'); 

} 
+0

請檢查cakephp插件https://github.com/CakeDC/recaptcha –

回答

0

嘗試像這樣進行驗證:

在您看來,下面的代碼添加到窗體:

<?php echo '<div class="g-recaptcha" data-sitekey=your-key></div>'; ?> 

然後,在位指示:

if (! empty($this->request->data['g-recaptcha-response'])) { 
    $captcha = $this->request->data['g-recaptcha-response']; 
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=your-secret&response=".urlencode($captcha)."&remoteip=".$_SERVER['REMOTE_ADDR']);  
    $response = json_decode($response); 

    if ($response->success) { 
    $captchaValidated = true; 
    } 
} 

if (!$captchaValidated) { 
    $this->Session->setFlash('Please verify you are not a robot'); 
} 

的doc:https://developers.google.com/recaptcha/docs/verify?hl=en