如何在codeigniter中集成谷歌reCAPTCHA?

时间:2013-11-04 11:10:00

标签: codeigniter

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

// 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

请在我出错的地方帮助我。

感谢。

5 个答案:

答案 0 :(得分:1)

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

if($resp->is_valid)

if($this->recaptcha->is_valid)

现在它对我有用。

感谢所有回答我问题的人。

答案 1 :(得分:1)

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";
    }
}

答案 2 :(得分:0)

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

$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.

答案 3 :(得分:0)

我正在使用 codeigniter 3.1.5 。我必须使用此代码,但不能为我工作,但这个代码适用于谷歌recaptcha在codeigniter 3.1.5谷歌recaptcha。

这是我在codeigniter中 google recaptcha

的代码
<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;
    }
}

Reference :: http://www.onlinecode.org/integrate-google-recaptcha-codeigniter-validation/

答案 4 :(得分:0)

我按照以下步骤轻松添加了Googe reCAPTCHA- 添加页面的此代码页脚。

<script src='https://www.google.com/recaptcha/api.js'></script>

然后将其添加到表单-

<div class="g-recaptcha" data-sitekey="6LceHSQTAAAAAB3KdV7X215KMq1jJnVaxF0rWfpO"></div>

由Google reCAPTCHA提供的6LceHSQTAAAAAB3KdV7X215KMq1jJnVaxF0rWfpO更改为您的更改。然后,将这些代码添加到控制器中。

$secret = 'recaptcha-secrate';
$response = $this->input->post('g-recaptcha-response');
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$response);
$responseData = json_decode($verifyResponse);
if(!$responseData->success){

$this->session->set_userdata(array('message'=>'<div id="message" class="animated shake alert alert-danger"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><h4><i class="icon fa fa-ban"></i> Alert!</h4> reCAPTCHA input is not valid.</div>'));
redirect($_SERVER['HTTP_REFERER'], 'refresh');

}

简单又酷。这是我网站login页上的实时示例。如果需要,您可以找到更多详细信息Here