Apr
19

How To: Use Re-Captcha in Codeigniter based website?


re-captcha in codeigniterRe-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.

Add To Facebook Stumble This Digg This Add To Del.icio.us Add To Reddit Add To Yahoo Add To Twitter


Written by ZeeShaN

ZeeShaN RasooL is a web developer who loves to work in latest technologies to create more interactive dynamic and beautiful web pages.

  • ashraf

    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

    November 24 2011
    CommentsLike
    • Nathan

      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?

      November 15 2011
      CommentsLike

      • [...] 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? [...]

        October 31 2011
        CommentsLike
        • ZeeShaN

          hmmm. good. :)

          August 02 2010
          CommentsLike
          • pravin

            I had fix my problem
            session->userdata(‘Mesg’)!=”){ echo $this->session->userdata(‘Mesg’); $this->session->unset_userdata(‘Mesg’);} ?>
            now it works prefectly.

            August 02 2010
            CommentsLike
            • pravin

              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.

              August 02 2010
              CommentsLike
              • ZeeShaN

                did you include recaptcha library ? how you use its name?

                July 26 2010
                CommentsLike
                • Richard

                  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

                  July 26 2010
                  CommentsLike
                  • Richard

                    Unable to load the requested file: helpers/recaptchalib_helper.php
                    Please help with that error

                    July 26 2010
                    CommentsLike
                    • SmacksBPO

                      Its prety looking and simple to use. I just use a library from CI forum and its a great and easy integration.

                      April 21 2010
                      CommentsLike








                      Enter your Email:

                      Click Here for Popular

                      Who I Am

                      Zeeshan Rasool

                      Software Engineer - PHP
                      Lahore - Pakistan

                      zeeshan(@)99points.info
                      Skype: zeeshan-rasool
                      gTalk: zishan.rasool85

                      Categories

                      Tags

                      Comments