添加验证码到php脚本(CakePhp)


Add reCaptcha code to php script (CakePhp)

我正试图添加谷歌的reCaptcha到我的一个页面。我一直在遵循本教程https://codeforgeek.com/2014/12/google-recaptcha-tutorial/,并完成了设置和表单站点上的所有内容。然而,我被困在PHP方面。我的代码在数据库中存储数据,然后发送电子邮件。我一直在尝试各种方法将验证码集成到现有的功能中,但没有运气。

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');  
}

验证如下:

在您的视图中,向表单添加以下代码:
<?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');
}

文档:https://developers.google.com/recaptcha/docs/verify?hl=en