谷歌Captcha Codeigniter控制器

I have a contact form that uses the CodeIgniter validation library and also AJAX to validate and submit the form. My next step was to integrate Google's Captcha into CI for my form. Now the official tutorial has a tutorial for standard php setup and i preferred attempting a CI way which i found an updated post here:

http://blog.russkern.com/integrating-recaptcha-into-codeigniter-forms/

I followed his instructions but i am unsure on how to implement the controller in terms of placing the function and if statement with my other AJAX / validation statements.

Has anyone had this problem before or is there a way to implement in what i already have? My form validation words for when the function is at the top but its integrating the rest in my controller.

Here is my code:

View:

/* Form code is here /*

require_once('php/recaptchalib.php');
$publickey = "my.public.key";
echo recaptcha_get_html($publickey);

Controller with captcha validation included:

class Contact extends CI_Controller {

function __construct() {
    parent::__construct();
    $this->load->library('session');
    $this->load->library('form_validation');
}

public function index() {

    $this->load->library('form_validation');

    $this->form_validation->set_rules('name','Name','trim|required|htmlspecialchars|max_length[30]|xss_clean');
    $this->form_validation->set_rules('email','Email Address','trim|valid_email|required|htmlspecialchars|max_length[100]|xss_clean');
    $this->form_validation->set_rules('message','Message','trim|required|htmlspecialchars|xss_clean');
    $this->form_validation->set_rules('recaptcha_challenge_field','challenge','trim|required|callback_captcha_check');
    $this->form_validation->set_error_delimiters('<div id="errors">&bull;&nbsp;','</div>');


    if($this->input->is_ajax_request()) {       
        $respond = array();
        if($this->form_validation->run() == FALSE) {
            $respond['result'] = 'false';
            $respond['errors'] = validation_errors();
        } else {
            $respond['result'] = 'true';
            $this->session->set_flashdata('success', 1);
            $respond['redirect'] = base_url().'contact';
        }
        return $this->output->set_output(json_encode($respond));
    } else {
        if($this->form_validation->run() == FALSE) {
            $respond['errors'] = validation_errors();   
        } else {
            $this->session->set_flashdata('success', 1);    
            redirect('contact');
        }
    }

        $data['page_title'] = 'Contact';
        $data['content'] = 'contact';   
        $this->load->view('template', $data);
} 


}

Here is what i need to put in my controller... when i put this above the index function the validation works so i know it will work but unsure how to integrate into controller:

function captcha_check($str) {
    require_once('php/recaptchalib.php');
    $privatekey = "private.key";
        $resp = recaptcha_check_answer ($privatekey,
        $_SERVER["REMOTE_ADDR"],
        $_POST["recaptcha_challenge_field"],
        $_POST["recaptcha_response_field"]);


    if (!$resp->is_valid) {

    $this->form_validation->set_message('captcha_check', 'The reCAPTCHA wasn\'t entered correctly. Go back and try it again.');
    return FALSE;

// What happens when the CAPTCHA was entered incorrectly
die ("The reCAPTCHA wasn’t entered correctly. Go back and try it again." .
"(reCAPTCHA said: " . $resp->error . ")");

    } else {
    echo 'hello';
    }

}

You can call a function from another function by using $this->function_name().

class Contact extends CI_Controller {

        function __construct() 
        {
          //Lines of code
        } 

        public function index()
        {
          //Lines of codes
          $this->captcha_check(); // Insert the code wherever you want it to be called
        }

        function captcha_check()
        {
          //Lines of codes
        }
}

I got this to work be refining, tidying and changing my keys as follows:

Changed my keys to use on the localhost ( at one point i had working code but would not throw the success message due to this.)

Put my recaptchalib file in a helper and loaded this in at the top with the other files such as libraries.

Put my public and private keys in a config file and loaded these in.

Used the same function as above but got the public and private keys as follows:

$data['html_captcha'] = recaptcha_get_html($this->config->item('publickey'));
$return = recaptcha_check_answer($this->config->item('privatekey'),
                                        $_SERVER["REMOTE_ADDR"],
                                        $this->input->post("recaptcha_challenge_field"),
                                        $this->input->post("recaptcha_response_field"));

if statement goes here...

And in my view: echo $html_captcha;