2013-11-04 67 views
3

我正在codeigniter 2.4中工作。我必須在我的項目中使用google recaptcha。以下是我的代碼。如何在codeigniter中集成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 

請幫我在哪裏,我錯了。

謝謝。

+0

您已經爲驗證碼的類?哪一行是59? –

+0

我已將該庫包含在構造函數中作爲 $ this-> load-> library('recaptcha'); – user2826169

+0

第59行是 if($ resp-> is_valid) – user2826169

回答

1

我已經更新了我的代碼,它現在對我的作品。在驗證碼庫我所做的is_valid公共財產,然後我更換

if($resp->is_valid) 

if($this->recaptcha->is_valid) 

現在它爲我工作。

感謝所有回覆我問題的人。

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

我工作在codeigniter 3.1.5。我必須使用此代碼,但不適用於我,但此代碼適用於谷歌recaptcha在谷歌recaptcha codeigniter 3.1.5。

這是笨我的谷歌驗證碼代碼谷歌的

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

功能驗證驗證碼

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/