user2826169
user2826169

Reputation: 285

how to integrate google reCAPTCHA in codeigniter?

I am working in codeigniter 2.4. I have to use google recaptcha in ony of my project.Below is my code.

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

The call back function is :

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

    }

But I am getting this error:

  A PHP Error was encountered

  Severity: Notice

  Message: Trying to get property of non-object

 Filename: controllers/offer.php

 Line Number: 59

Please help me where I am going wrong .

Thanks.

Upvotes: 7

Views: 18080

Answers (4)

darpan chavhan
darpan chavhan

Reputation: 21

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

Upvotes: 2

JON
JON

Reputation: 1013

I am working in codeigniter 3.1.5. I have to use this code but not work for me but this code work for me in google recaptcha in google recaptcha in codeigniter 3.1.5.

this is my code for google recaptcha in codeigniter

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

function of google validate captcha

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/

Upvotes: 0

user2826169
user2826169

Reputation: 285

I have updated my code and it works for me now. In the captcha library I have made the is_valid property public and then I replaced

if($resp->is_valid)

with

if($this->recaptcha->is_valid)

Now it works for me.

Thanks for all who responed my question.

Upvotes: 2

Nil&#39;z
Nil&#39;z

Reputation: 7475

You need to provide your private key in here too as the first parameter:

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

Upvotes: 1

Related Questions