Posted by Thurein on 4th March 2011

reCAPTCHA plugin for php codeigniter

I’m playing around with codeigniter frameworks these days. One of my client requested to put reCAPTCHA into CI frameworks. So, I have tried to convert from original reCAPTCHA php plug-in to codeigniter library. So, I would like to share this library. I hope it useful for someone.

To use this library, you just need to follow by under following instruction :

Firstly, you have to move Recaptcha.php file into CI file structure system/application/libraries/
Use following code to load that library -

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

Then you have to set keys. Sometime you just need to set only one public key. So, just set public key only.
To set the keys

// Get a key from https://www.google.com/recaptcha/admin/create
$this->recaptcha->set_key ('XXXXXXXXXX Key XXXXXXXXX' ,'public') // For public key
$this->recaptcha->set_key ('XXXXXXXXXX Key XXXXXXXXX' ,'private') // For private key


To Show reCAPTCHA image and from. You just simply need to call recaptcha_get_html () function. In that function you can define theme and language of reCAPTCHA (Customization).
The function builded like -

recaptcha_get_html ( $error = null, $use_ssl = false, $options = array ('theme' => 'red', 'lang' => 'en') )

You just simply need to call like -

//Options for themes and language
$options = array ('theme' => 'clean', 'lang' => 'fr') ;
echo $this->recaptcha->recaptcha_get_html ( '', false, $options )

To check the answer from client

$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) {
                echo "You got it!";
} else {
                # set the error code so that we can display it
                $error = $resp->error;
}

For mail hide functions

To use mail hide function, set mail hide api keys -

// Get a key from https://www.google.com/recaptcha/admin/create
$this->recaptcha->set_key ('XXXXXXXXXX Key XXXXXXXXX' ,'public') // For public key
$this->recaptcha->set_key ('XXXXXXXXXX Key XXXXXXXXX' ,'private') // For private key

The Mailhide version of example@example.com is

echo $this->recaptcha->recaptcha_mailhide_html ("example@example.com");

The url for the email is:

echo $this->recaptcha->recaptcha_mailhide_url ("example@example.com");

Download reCAPTCHA Codeigniter Library ( size 4 kB)

Hope it’s useful. Enjoy !!!

  • About Thurein Soe
    Visit Thurein's website.

    Freelance Web Developer. Founder of The Tech Space. Follow me on Twitter.

    1. 5 Responses

    2. Ko Ellay says:

      Useful post but i am curious to know how to set up public key. I see XXXXX here but is it something we can give our own number/text or something we can ONLY get from Google?

      Thank you.

    3. Thurein says:

      Yes, you can get those key from there
      https://www.google.com/recaptcha/admin/create
      Just replace xxxxx with those keys.

    4. Ko Ellay says:

      Thanks for quick response. I may need to create one soon for my WP blog. Have a good day.

    5. Mike says:

      Thanks for the library. I have one problem, in that if the form is submitted without anything in the captcha field then $resp is an array instead of an object.

    6. Thurein says:

      Thanks Mike, I will have a look and fix it.

    Post your comments