【问题标题】:Validate recaptcha ajax and codeigniter验证 recaptcha ajax 和 codeigniter
【发布时间】:2019-01-21 21:14:47
【问题描述】:

我正在使用 Codeigniter 和 Ajax 来验证和提交电子邮件表单。 我尝试了很多方法,它一直给我结果 {"sent":false} 在我添加 recaptcha 行之前它工作得很好。

这是我的代码:

custom.script.js

$("#ajax-contact-form").submit(function() {
    var href = 'http://localhost:8888/en/send';
    var name = $("#form-name").val();
    var email = $("#email").val();
    var phone = $("#phone").val();
    var title = $("#form-title").val();
    var message = $("#form-message").val();
    $.ajax({
        type: "POST",
        url: href ,
        dataType: "json",
        data: {
            name: name,
            email: email,
            phone: phone,
            title: title,
            message: message,
            //UPDATE from recaptcha without quotes to 'g-recaptcha-response'
            'g-recaptcha-response': grecaptcha.getResponse()
          },

        success: function(msg) {
            if(msg.sent) {
                console.log("Successssssssssss");
                $('#result').addClass('success'); 
                $('#result').html('Email was sent successfully!');
            } else {
                $('#result').addClass('error'); 
                $('#result').html('Email was NOT sent!'); 
            }
        }
    });
    return false;
});

En.php

function send()
{
    $this->load->helper('form');
    $this->load->library('form_validation');

    $google_url="https://www.google.com/recaptcha/api/siteverify";
    //UPDATE from recaptcha to 'g-recaptcha-response'
    $str = $this->input->post('g-recaptcha-response');
    $secret = 'GOOGLE_SECRET_KEY';
    $url=$google_url."?secret=".$secret."&response=".$str;
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $output = curl_exec($ch); 
    curl_close($ch);      
    $status= json_decode($output, true);


    if ($status['success']) {


    $this->load->library('email');

    $config['mailtype'] = 'html';
    $config['charset'] = 'utf-8';
    $config['wordwrap'] = TRUE;

    $this->email->initialize($config);

    $body    = 'ANY TEXT';
    $emailto = "email@domain.com";

    $this->email->from($this->input->post("email"), $this->input->post("name"));
    $this->email->to($emailto);
    $this->email->subject('New Request');
    $this->email->message($body);

    if($this->email->send()){
    echo json_encode(array("sent"=>TRUE));
    }

}
else{
    echo json_encode(array("sent"=>FALSE));
}}

谷歌键是正确的,我不知道为什么它没有得到真实的 if 语句。

【问题讨论】:

    标签: ajax codeigniter codeigniter-3 recaptcha


    【解决方案1】:

    如果你在给定的实现中使用'g-recaptcha-response'并且$status['success']返回null,最好使用如下cURL请求的方法:

    $google_url = "https://www.google.com/recaptcha/api/siteverify";
    $secret_key = 'YOUR_SECRET_KEY';
    
    $response = $_POST['g-recaptcha-response']; 
    
    $message  = 'Google reCaptcha Test';
    
    if(!empty($response))
    {
        $url = $google_url."?secret=".$secret_key."&response=".$response;
    
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_TIMEOUT, 15);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, TRUE); 
        $curlData = curl_exec($curl);
    
        curl_close($curl);
    
        $res = json_decode($curlData, TRUE);
        if($res['success'] == 'true') 
            $message = "Success!";
        else
            $message = "Enter captcha again!";
    }
    

    【讨论】:

    • 像魅力一样工作 :) 谢谢
    • 很高兴为您提供帮助。在这里看到这些反馈也很棒。谢谢
    【解决方案2】:

    您好,我们来讨论一下。似乎返回的数据 $status['success'] 需要使用“==”运算符与所需值进行比较。你可以这样检查。

    if ($status['success'] == true) {
    
    
    $this->load->library('email');
    
    $config['mailtype'] = 'html';
    $config['charset'] = 'utf-8';
    $config['wordwrap'] = TRUE;
    
    $this->email->initialize($config);
    
    $body    = 'ANY TEXT';
    $emailto = "email@domain.com";
    
    $this->email->from($this->input->post("email"), $this->input->post("name"));
    $this->email->to($emailto);
    $this->email->subject('New Request');
    $this->email->message($body);
    
    if($this->email->send()){
    echo json_encode(array("sent"=>TRUE));
    }
    

    }

    另外,请在将其置于条件(if else 语句)之前区分值。

    【讨论】:

    • 嘿,$status['success'] 有返回值吗?
    • 它从来没有进入那个if语句,我只是看到很多人在使用它我仍然不知道它会变成什么我会尝试检查
    • 您必须检查 if 语句。因为正如您在帖子中所说,您收到的是"sent"=>FALSE,所以这意味着代码流动它不接受您的 if 语句的条件。
    • 它正在返回:null
    • 即使在提交表单并标记recaptcha之后它仍然是空的
    【解决方案3】:

    终于成功了,这是最终代码:

    function send()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');
    
    
    $google_url="https://www.google.com/recaptcha/api/siteverify";
    $str = $this->input->post('g-recaptcha-response');
    
    
    
    $secret = 'GOOGLE SECRET KEY';
    $url=$google_url."?secret=".$secret."&response=".$str;
    
    
    $response = json_decode(file_get_contents($url), true);
    if ($response["success"] != false) {
    
    
    
    
        $this->load->library('email');
    
        $config['mailtype'] = 'html';
        $config['charset'] = 'utf-8';
        $config['wordwrap'] = TRUE;
    
        $this->email->initialize($config);
    
        $body    = 'ANY TEXT';
    
        $emailto = "email@domain.com";
    
        $this->email->from($this->input->post("email"), $this->input->post("name"));
        $this->email->to($emailto);
    
        $this->email->subject('New Request');
        $this->email->message($body);
    
        if($this->email->send()){
        echo json_encode(array("sent"=>TRUE));
        }
    }
        else{
        echo json_encode(array("sent"=>FALSE));
         }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多