google codeigniter 中的 recaptcha 错误 php 提交表单时

google recaptcha error in codeigniter php while submiting form

我已经在 codeigniter 中创建了一个表单并给出了一个 google 验证码 v2,我已经创建了站点密钥和密钥并将其添加到配置文件中,还添加了 js 和 recaptcha div 包括我的网站密钥。以下是我在控制器中的验证码功能:

public function validate_captcha() {
          $recaptcha = trim($this->input->post('g-recaptcha-response'));
          $userIp= $this->input->ip_address();
          $secret='xxxxxxxxxxxx'; (i have given my scret key here)
          $secretdata = array(
              'secret' => "$secret",
              'response' => "$recaptcha",
              'remoteip' =>"$userIp"
          );

          $verify = curl_init();
          curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
          curl_setopt($verify, CURLOPT_POST, true);
          curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($secretdata));
          curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
          $response = curl_exec($verify);
          $status= json_decode($response, true);
          if(empty($status['success'])){
              return FALSE;
          }else{
              return TRUE;
          }
      }

以下是我在同一控制器中的注册表单函数:

public function ajaxRegAction() {
   $this->load->library('session');
  header('Access-Control-Allow-Origin: *');
  header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
  header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
  $this->load->library('form_validation');
  $utype='W';
    $dhamu = $this->validate_captcha();
  $createddate  = date('Y-m-d h:i:s');
  $createdby = '0';
  $mobile = $this->input->post('phone');
  $form_data = array(
    'type' => $utype,
    'unique_id' => $this->mainModel->generateIndividualID(),
    'phone_num' => $mobile,
    'email' => $this->input->post('email'),
    'first_name' => $this->input->post('firstname'),
    'last_name' => $this->input->post('lastname'),
    'created_by' => $createdby,
    'created_date' => $createddate,
  );
  $name = $this->input->post('firstname')." ".$this->input->post('lastname');
  $access_info = array('user_type'=>$utype);

   $check = $this->mainModel->checkUserAvail($this->input->post('phone'),$this->input->post('email'));
   $checkauser = $this->mainModel->checkAjaxUser($this->input->post('phone'),$this->input->post('email'));
   if($check==0) {

        if($dhamu==1){

    $insert = $this->mainModel->ajaxRegInsertUser($form_data, $access_info,$this->input->post('password'));

    $message="Dear ".$this->input->post('firstname').", You have successfully registered with Book The Party. Thank You for coming On-Board. Contact us at 9666888000 for any queries - Team BTP";
    $email_message=$message;
    $message=rawurlencode($message);
    $this->mainModel->sendOtptoCustomer($mobile,$message);
    $this->mainModel->sendmailtoCustomer($this->input->post('email'),$email_message);

echo "success";
}

else{ echo "Captcha Error";}



}

else{
echo "Registration Failed !";

   }

即使我选中 google recaptcha 框并按注册,表单显示 "Captcha Error",值也不会添加到数据库中。谁能告诉我这里可能出了什么问题,在此先感谢

第一步: 确保您已在 Google Captcha V2 仪表板中的域中添加 localhost

第 2 步:

我正在修改你的功能,你可以这样使用它:

public function validate_captcha() 
{
    if(isset($_POST['g-recaptcha-response']))
    {
        $captcha=$_POST['g-recaptcha-response'];
    }

    $secretKey = "Put your secret key here";
    $ip = $_SERVER['REMOTE_ADDR'];
    // post request to server
    $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) .  '&response=' . urlencode($captcha);
    $response = file_get_contents($url);
    $responseKeys = json_decode($response,true);
    // should return JSON with success as true
    if($responseKeys["success"]) {
        return TRUE;
    } else {
        return FALSE;
    }
}

而不是 CURL

如果可行请告诉我