How To: Use Re-Captcha in Codeigniter based website?
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.
Who I Am

Zeeshan Rasool
Software Engineer - PHP
Lahore - Pakistan
zeeshan(@)99points.info
Skype: zeeshan-rasool
gTalk: zishan.rasool85
Categories
- AJAX (37)
- Codeigniter (16)
- CSS (16)
- Facebook (11)
- Joomla (1)
- JQuery (53)
- Miscellaneous (4)
- Mootools (1)
- MySQL (6)
- PHP (60)
- SEO (2)
- Technology (6)
- Tutorials (15)
- Twitter (2)
- Web Design (23)
- Web Development (57)
- WordPress (2)
Tags
Comments
- ZeeShaN on Facebook Wall Script Clone with JQuery and PHP: Personal Project BETA Version 2.0
- 20 + Fresh jQuery Image Gallery/Slider Plugins and Tutorials Worth a Look | free on JQuery Based Flipped Image Gallery with Bounce Effects
- 20 + Fresh jQuery Image Gallery/Slider Plugins and Tutorials Worth a Look | free on Fresh JQuery Image Gallery with Captions and Auto Play/Pause Rotation
- 25 Cool and Helpful jQuery Plugins/Tutorials For Your Next Project | free on jQuery Tutorial: Create jQuery and CSS based Awesome navigation.
- 25 Cool and Helpful jQuery Plugins/Tutorials For Your Next Project | free on Ajax Tutorial: How to Create Ajax Search Using PHP jQuery and MYSQL
ZeeShaN






Good tutorial but unfortunately it is incomplete, which most likely will leave beginner in the dark.
Nathan,
To include captcha challenge into your html you need to load recaptcha helper first and then call this function : recaptcha_get_html($pubkey)
you must get your public key from the recaptcha site and pass it to that function
e.g.
$this->load->helper(recaptcha);
$pubkey = ‘xxxxxxx’
$data['html_captcha'] = recaptcha_get_html($pubkey);
$this->load->view(‘form’,$data);
that’s the easy way to include into your view
Please post your source. First, after renaming the library and placing it in the helpers folder, it does work I assume, but how the hell do I call it onto my view?
[...] Payment Gateway in CodeIgniter? 30 Top CodeIgniter best Tutorials You must want to know. How To: Use Re-Captcha in CodeIgniter based website? CodeIgniter 2.0 ?? Whats Great in New version How can We verify Captcha with Ajax in CodeIgniter? [...]
hmmm. good.
I had fix my problem
session->userdata(‘Mesg’)!=”){ echo $this->session->userdata(‘Mesg’); $this->session->unset_userdata(‘Mesg’);} ?>
now it works prefectly.
i had i problem in CI that when i input incorrect captcha it doesnot show message on the form .So please can you solve this problem with code on the view part.
did you include recaptcha library ? how you use its name?
When i try to load the $this->load->helper(‘recaptchalib’)
it show me the error Unable to load the requested file: helpers/recaptchalib_helper.php
Unable to load the requested file: helpers/recaptchalib_helper.php
Please help with that error
Its prety looking and simple to use. I just use a library from CI forum and its a great and easy integration.