How to use reCaptcha in CodeIgniter

This is a widely used captcha implementation. It’s used at big sites like Facebook and CraigsList. I have some porblem to use that . But from the forum i get it very easily.
Here is the way to use reCaptcha helper in CodeIgniter.
Steps to Use
1. Sign up for an access key at http://recaptcha.net/
2. Get recaptchalib.php from the zip archive located at http://code.google.com/p/recaptcha/downloads/list?q=label:phplib-Latest.
3. Rename recaptchalib.php to recaptcha_helper.php and put it in your helpers folder.
4. Add the following function to recaptcha_helper.php:
function recaptcha() { $CI =& get_instance(); $CI->config->load('recaptcha'); $public_key = $CI->config->item('recaptcha_public_key'); $message = isset($CI->validation->recaptcha_error) ? $CI->validation->recaptcha_error : ''; return recaptcha_get_html($public_key, $message); }

5. Add the following function to your MY_Validation class as seen below in the libraries folder. (If you don’t already have one a MY_Validation class, make one.)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Validation extends CI_Validation { function MY_Validation() { parent::CI_Validation(); } function recaptcha_matches() { $CI =& get_instance(); $CI->config->load('recaptcha'); $public_key = $CI->config->item('recaptcha_public_key'); $private_key = $CI->config->item('recaptcha_private_key'); $response_field = $CI->input->post('recaptcha_response_field'); $challenge_field = $CI->input->post('recaptcha_challenge_field'); $response = recaptcha_check_answer($private_key, $_SERVER['REMOTE_ADDR'], $challenge_field, $response_field); if ($response->is_valid) { return TRUE; } else { $CI->validation->recaptcha_error = $response->error; $CI->validation->set_message('recaptcha_matches', 'The %s is incorrect. Please try again.'); return FALSE; } } }

5. Create a recaptcha.php in your config folder and add your public and private keys (the ones you got from registering) as seen below:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); $config['recaptcha_public_key'] = "..."; $config['recaptcha_private_key'] = "...";

Example usage in a controller:

public function register() { $this->load->library('validation'); $this->load->helper('recaptcha'); $rules['username'] = 'trim|required|unique[users.username]'; $rules['password'] = 'required|password|matches[password_confirm]'; $rules['password_confirm'] = 'required'; $rules['email'] = 'trim|required|valid_email|unique[users.email]'; $rules['recaptcha_challenge_field'] = 'required|recaptcha_matches'; $this->validation->set_rules($rules); $fields['username'] = 'username'; $fields['password'] = 'password'; $fields['password_confirm'] = 'password confirmation'; $fields['email'] = 'email'; $fields['recaptcha_challenge_field'] = 'answer to the security question'; $this->validation->set_fields($fields); if ($this->validation->run() == FALSE) { $this->load->view('user_registration'); } else { echo 'Success!'; } }

In the view, you can create the captcha form element by echoing <?= recaptcha() ?> I can package all of this into a zip file upon request. Here is the original link .

About these ads
  1. This is useful for me. Thanks a lot!

  2. Thanks A lot brother, you really help me

  3. thanks bro

    • Muthuraj
    • June 18th, 2009

    Sir I tried your code but I cant get the answer i didn’t know what mistake that I have done, so can you please send me that zip file package, I think it may be useful for me.

    • Faruque
    • July 6th, 2009

    Brother, Please send package into a zip file.
    i will be very grateful to you

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: