How To: Use Re-Captcha in Codeigniter based website?
[ad#co-4]
Re-captcha is a free web service to add a secured captcha on your website to avoid spams. Spams filled up your inboxes with unnecessary emails or messages which you have no need. Re-captcha is provided by Carnegie Mellon University which are serving over 60 millions captchas in a day.
Here are some simple steps to complete this whole process.
1- Just go to re-captcha sign up page and enter your information. You need to add your site url there. And each of your site which you need to be deployed with re-captcha must have its domain name on your account in re-captcha site.
2- You will get public and private kep after entering your domain name and clicking on Create Key button. Save these codes into a notepad.
3- Download re-captcha library (recaptchalib.php) which will be used to interact with re-captcha API.
4- We will use this library as helper in codeigniter. Because this library holds a list of function which interacts re-captcha API. Paste this helper in to your system/application/helpers folder.
5- Now the work has been done at configuration level. Call your helper in controller.
1 |
$this->load->helper('recaptchalib'); |
6- In your controller, paste this code. Here the recaptcha_check_answer is used to get what code a user entered.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
function recaptcha_validation($string) { $return = recaptcha_check_answer($this->config->item('private_recaptcha_key'), $_SERVER["REMOTE_ADDR"], $this->input->post("recaptcha"), $this->input->post("recaptcha")); if(!$return->is_valid) { $this->session->set_userdata("Mesg",'Code entered is invalid !'); return FALSE; } else { return TRUE; } } function login_user_function() { $this->load->library('validation'); $this->load->helper('recaptcha_helper'); $rules['recaptcha'] = 'required|callback_recaptcha_validation'; $this->validation->set_rules($rules); $fields['recaptcha'] = 'Code'; $this->validation->set_fields($fields); if($this->validation->run() == FALSE) { // code is not correct } else { // Code is verified now submit the form } } |
In codeigniter we use config file to hold some global constant types values so i put private recaptcha key (private_recaptcha_key) in config file.
[ad#co-1]

Author of 99Points
Zeeshan Rasool is an experienced PHP Web Developer and founder at 99Points.
Expert in Codeigniter (MVC), WordPress, JSON, jQuery & HTML5