Display Google Recaptcha on Codeigniter website with 6 Lines of new code

Google Recaptcha is one of the most effective ways of fighting automated spam today. Below we will see how we can integrate google recaptcha  on a codeigniter website in just three steps :

  • Go to https://www.google.com/recaptcha/admin and sign up for reCAPTCHA API keys for your website. The keys automatically work on http://localhost or There are two reCAPTCHA keys: Site Key and Secret Key. Site Key would be used in the front-end public form and  Secret Key will be used in the back-end code (php, ruby etc).
  • Open the view page of your codeigniter where the form is located. Let’s assume we have a form to collect some comments from end user similar to below page:
     <meta charset="UTF-8">
     <title><?php echo $title; ?></title>
     <script src='https://www.google.com/recaptcha/api.js'></script>
<?php echo validation_errors(); ?> 
<?php echo form_open('comments/create'); ?> 
<label for="name">Your Name</label> 
<input name="name" type="input" /> 
<label for="comment">Comment</label> 
<textarea name="comment"></textarea>
<div class="g-recaptcha" data-sitekey="your-public-site-key"></div>
<input type="submit" name="submit" value="Comment" />

We only added 2 lines here, highlighted in bold. If your key is correct, you should be seeing google reCAPTCHA widget before submit while viewing the form.

google recaptcha image

Now we will add few lines in the controller/model code in the back end.

  • Go to the controller comments/create and add the lines as below where form validation is being performed
$this->form_validation->set_rules('g-recaptcha-response', 'Captcha', 'required'); 
if (($this->form_validation->run() === FALSE) 
    //Store the response from form POST in a variable
    $captcha = $this->input->post('g-recaptcha-response');
    //Make a call to Google API to verify the user response
    // Below line decodes the JSON response from above url 
        //If the validation is successful, proceed with your logic
        $data = array( 'name' => $this->input->post('name'), 
        'comment' => $this->input->post('comment') );
        echo 'Your Name: ' . $data['name'] ;
        echo 'Comment: ' . $data['comment'] ;

That’s it. Let us know using the comments below if any issues !!!

Updated: March 5, 2015 — 10:02 am


Add a Comment
  1. I have several CodeIgniter sites where we’d like to implement the reCaptcha method exactly like how you’ve done this form, where the submit button is greyed out until the user verifies they are human. How do you do that?

    1. Hi Dale,
      If you follow the above tutorial, you will be able to create a reCaptcha form exactly as on this page. Firstly, sign up for a google recapta account as above, write code in your view and controller/method as shown above and you should be good.
      Make sure you use your own keys while creating/validating the form.
      If you further face issues, let me know the steps you have taken and issue being faced, I will be happy to assist.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.