Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to integrate google reCAPTCHA in codeigniter?

Tags:

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.

like image 875
user2826169 Avatar asked Nov 04 '13 11:11

user2826169


People also ask

How do I validate Captcha?

Click the checkbox to get a verification challenge. The checkbox has been clicked and a challenge is loading. You are instantly verified if the status changes to “You are verified”. Otherwise, you are required to complete a verification challenge.


2 Answers

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.

like image 120
user2826169 Avatar answered Oct 01 '22 10:10

user2826169


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";
    }
}
like image 44
darpan chavhan Avatar answered Oct 01 '22 11:10

darpan chavhan