jQuery Ajax Recaptcha无法使用Codeigniter

时间:2019-02-24 17:27:22

标签: php jquery codeigniter recaptcha

视图:

<script>
    $(document).ready(function(){
        $("#register").click(function(e) {
            e.preventDefault();
            name = $("#name_up").val();
            $.ajax({
                type: "POST",
                url: "<?php echo base_url(); ?>register",
                data: {"name":name, "g-recaptcha-response": grecaptcha.getResponse()},
                success: function(data) {
                    alert(data);
                }
            });
        });
    });
</script>

<form method="post" id="contact-form-up">
    <input type="text" name="name_up" id="name_up" placeholder="Full Name">
    <div class="g-recaptcha" data-sitekey="****************************************"></div>
    <input class="theme-btn submit-btn" type="submit" name="register" id="register" value="Sign Up">
</form>

控制器:

public function register()
{
    $recaptcha = $this->input->post('g-recaptcha-response');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,"secret=****************************************=".$recaptcha);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $server_output = curl_exec ($ch);
    $server_output = json_decode($server_output,true);
    curl_close ($ch);
    if($server_output['success'])
    {
        echo "captacha validated successfully";
    }
    else
    {
        echo "invalid captcha";
    }
}

在此代码中,我有一个使用Google Recaptcha的表单,当我单击register按钮时没有检查Recaptcha,然后它提醒invalid captcha,但是当我再次检查Recaptcha时,它显示invalid我不知道为什么我在哪里做错了?请帮助我。

谢谢

0 个答案:

没有答案