Codeigniter Google Recaptcha Form Validation Example

By Hardik Savani November 5, 2023 Category : PHP jQuery Codeigniter

In this tutorial, i would like to share with you how to integrate google recaptcha in codeigniter 3 application. I will show you step by step implementation of google new reCAPTCHA in codeigniter 3 app.

In today field, security is more important for every project even it is on java, php, .net etc. If you are creating login form for your project, then it can change to hack like someone can fire lot's of curl request etc. So at that moment if you use google captcha then it provide best security for prevent curl request ect. So, in this tutorial we will learn how to integrate google recaptcha in codeigniter app.

You have to just follow few steps and you will get layout like as bellow screeshot.


Step 1: Create Google App

In this step we have to create google app and then we will use google key and google secret on codeigniter app. So go here: Google Recaptcha Admin.

Screen 1:

Screen 2:

Ok now you generated api key and secret so, you have to just add that on config file like this way:


$config['google_key'] = 'YOUR_GOOGLE_KEY';

$config['google_secret'] = 'YOUR_GOOGLE_SECRET';

Step 2: Create Route

In this step we need to add two routes for render view and another for POST method. so open routes.php file and add code like as bellow:


$route['form'] = "FormController";

$route['formPost']['post'] = "FormController/formPost";

Step 3: Create FormController Controller

In this step we have to create new method in FormController Controller, in this file we will write validation logic.

So, create new method on this controllers folder and put bellow code:



defined('BASEPATH') OR exit('No direct script access allowed');

class FormController extends CI_Controller {


* Get All Data from this method.


* @return Response


public function __construct() {






* Get All Data from this method.


* @return Response


public function index()





* Get All Data from this method.


* @return Response


public function formPost()


$recaptchaResponse = trim($this->input->post('g-recaptcha-response'));


$secret = $this->config->item('google_secret');


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);


$status= json_decode($output, true);

if ($status['success']) {

print_r('Google Recaptcha Successful');



$this->session->set_flashdata('flashError', 'Sorry Google Recaptcha Unsuccessful!!');


redirect('form', 'refresh');



Step 4: Create View File

In last step, we have to create view file, we will create new view file "formPost.php" on views folder and put bellow code on that file.


<!DOCTYPE html>



<title>Google Recaptcha Code in Codeigniter 3 -</title>

<link rel="stylesheet" href="" />

<script src=''></script>



<div class="container">

<div class="card">

<div class="card-header">

Google Recaptcha Code in Codeigniter 3 -


<div class="card-body">

<form action="/formPost" method="POST" enctype="multipart/form-data">

<div class="text-danger"><strong><?=$this->session->flashdata('flashError')?></strong></div>

<div class="form-group">

<label for="exampleInputEmail1">Email address</label>

<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">


<div class="form-group">

<label for="exampleInputPassword1">Password</label>

<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">


<div class="g-recaptcha" data-sitekey="<?php echo $this->config->item('google_key') ?>"></div>


<button class="btn btn-success">Login</button>







Now it's all done steps.

I hope it can help you...How it works...