In this tutorial, We will learn how to integrate Google ReCaptcha in Codeigniter 3.We will learn step-by-step implementation of google's new reCAPTCHA in Codeigniter 3.






Step 1: 

Create google Recaptcha app  Google Login to Recaptcha Admin console.






Generated API key and secret so, You have to add that on the config file like application/config/config.php

<?php

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



Step 2: 

Create Route application/config/routes.php

<?php

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



Step 3: 

Create FormController Controller application/controllers/FormController.php

<?php
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() {
       parent::__construct();
       $this->load->helper('url');
       $this->load->library('session');
    }
 
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
    public function index()
    {
        $this->load->view('formPost');
    }
 
    /**
     * Get All Data from this method.
     *
     * @return Response
    */
    public function formPost()
    {
        $recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
 
        $userIp=$this->input->ip_address();
     
        $secret = $this->config->item('google_secret');
   
        $url="https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$recaptchaResponse."&remoteip=".$userIp;
 
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        $output = curl_exec($ch); 
        curl_close($ch);      
         
        $status= json_decode($output, true);
 
        if ($status['success']) {
            print_r('Google Recaptcha Successful');
            exit;
        }else{
            $this->session->set_flashdata('flashError', 'Sorry Google Recaptcha Unsuccessful!!');
        }
 
        redirect('form', 'refresh');
    }   
}


Step 4: 

Create view application/views/formPost.php

<!DOCTYPE html>
<html>
<head>
    <title>Google Recaptcha Code in Codeigniter 3</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" />
    <script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
  
<div class="container">
    <div class="card">
        <div class="card-header">
            Google Recaptcha Code in Codeigniter 3
        </div>
        <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>
  
                <div class="form-group">
                    <label for="exampleInputPassword1">Password</label>
                    <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
                </div>
                    
                <div class="g-recaptcha" data-sitekey="<?php echo $this->config->item('google_key') ?>"></div> 
                <br/>
                <button class="btn btn-success">Login</button>
            </form>
        </div>
    </div>
</div>
  
</body> 
</html>

Finally, You have to run your application


https;//localhost/your_ci_project/post