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

我在 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,

);

即使我检查了 google recaptcha 框并按注册,表单显示“Captcha 错误”,值也没有被添加到数据库中。谁能告诉我这里可能出了什么问题,在此先感谢



紫衣仙女
浏览 101回答 1
1回答

慕容3067478

第 1 步:确保您已localhost在 Google Captcha V2 仪表板中添加您的域。第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;    }}而不是卷曲如果这有效,请告诉我
打开App,查看更多内容
随时随地看视频慕课网APP