如何在codeigniter中集成google reCAPTCHA?

q35jwt9p  于 2022-12-07  发布在  Go
关注(0)|答案(4)|浏览(146)

我在codeigniter 2.4工作。我必须在我的项目中使用google recaptcha。下面是我的代码。

// field validation
$this->form_validation->set_rules('recaptcha_challenge_field', 'Captcha Code', 'trim|required|xss_clean|callback_checkCaptcha');

回调函数为:

function checkCaptcha($captcha){
    $resp = $this->recaptcha->recaptcha_check_answer ( $this->input->ip_address(), $this->input->post('recaptcha_challenge_field',true), $this->input->post('recaptcha_response_field',true));

    if($resp->is_valid)
    {
        return true;
        }

    else 
    {
         $this->form_validation->set_message('checkCaptcha', 'Sorry Invalid captcha code');
         return false;
        }

    }

但我得到这个错误:

A PHP Error was encountered

  Severity: Notice

  Message: Trying to get property of non-object

 Filename: controllers/offer.php

 Line Number: 59

请帮助我哪里出错了。

  • 谢谢-谢谢
9nvpjoqh

9nvpjoqh1#

我已经更新了我的代码,现在它对我有效了。在验证码库中,我已经将is_valid属性设置为public,然后我替换了

if($resp->is_valid)

if($this->recaptcha->is_valid)

现在对我有用了。
感谢所有回答我问题的人。

ee7vknir

ee7vknir2#

public function captcha_verify(){
    $form_response = $this->input->post('g-recaptcha-response');
    $url = "https://www.google.com/recaptcha/api/siteverify";

    $secretkey = "6LeBGG0UAAAAAEzWMaT0sOjPxcbNwQe7TiWWAknQ";

    $response = file_get_contents($url."?secret=".$secretkey."&response=".$form_response."&remoteip=".$_SERVER["REMOTE_ADDR"]);

    $data = json_decode($response);
    print_r($data);

    if (isset($data->success) && $data->success=="true") {
        echo "Successfully Passed through captcha";
    }
    else{
        echo "Please Fill captcha";
    }
}
js4nwp54

js4nwp543#

您还需要在此处提供您的私钥作为第一个参数:

$resp = $this->recaptcha->recaptcha_check_answer ($private_key,  $this->input->ip_address(), $this->input->post('recaptcha_challenge_field',true), $this->input->post('recaptcha_response_field',true));
echo "<pre>";print_r($resp);die; #check the response array.
5kgi1eie

5kgi1eie4#

我在codeigniter 3.1.5工作。我必须使用这个代码,但不为我工作,但这个代码为我在谷歌recaptcha在谷歌recaptcha在codeigniter 3.1.5工作。
这是我的代码为谷歌资本重组在codeigniter

<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-sitekey="ADD_YOUR_GOOGLE_SITE_KEY_HERE"></div>

google验证验证码功能

function google_validate_captcha() {
    $google_captcha = $this->input->post('g-recaptcha-response');
    $google_response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=your secret key here &response=" . $google_captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
    if ($google_response . 'success' == false) {
        return FALSE;
    } else {
        return TRUE;
    }
}

第一次

相关问题