Codeigniter PHP Google Recaptcha Form Validation Example
Codeigniter PHP Google Recaptcha Form Validation Example – In this tutorial, we’ll show you how to use Google reCaptcha with PHP CodeIgniter to prevent spamming on your forms. We’ll guide you through integrating Google APIs for reCaptcha validation. So, you’ll be able to make your forms more secure with Google Captcha.
To protect your website from spam and abuse, we’ll show you how to validate forms using Google reCaptcha. We’ll integrate Google reCaptcha with a codeigniter form and guide you through the process step-by-step.
Codeigniter Google reCaptcha

Contents
- Download Codeigniter Latest
- Basic Configurations
- Create Database With Table
- Setup Database Credentials
- Generate reCaptcha Key & Secret
- Make New Controller
- Create Views
- Start Development server
- Conclusion
Download Codeigniter Latest
In this next step, we’re going to download the newest version of Codeigniter. To do this, go to the website Download Codeigniter and get the most up-to-date setup. After you’ve downloaded it, unzip the setup in your local system’s xampp/htdocs/ folder. You can also rename your project folder to whatever you’d like.
Basic Configurations
Now it’s time to do some basic configuration settings on the config.php file. To do this, we’ll navigate to the application/config/config.php file and open it up in a text editor.
set base url like this:-
$config['base_url'] = 'http://localhost/demo/';
Create Databas with table
For this next step, we’ll need to create a database with the name “demo” To do this, we’ll open up PHPMyAdmin and create the database with that name. Once it’s successfully created, we can use the following SQL query to create a table in the database.
CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
name varchar(100) NOT NULL COMMENT 'Name',
email varchar(255) NOT NULL COMMENT 'Email Address',
contact_no varchar(50) NOT NULL COMMENT 'Contact No',
created_at varchar(20) NOT NULL COMMENT 'Created date',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table' AUTO_INCREMENT=1;
Setup Database Credentials
In the following step, we’ll connect our CRUD app project to the database. To do this, we’ll go to the
application/config/database.php
file and set up our database credentials. In this file, we’ll add our database name, username, and password.
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'demo',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Generate reCaptcha Key & Secret
We’ll now create a new Google reCaptcha key and secret for our application. Follow these simple steps to create your own Google reCaptcha key or secret:
- Click the link provided here to create your Google reCaptcha key or secret.
- This will open a form in your browser.
- Fill in the required information in the form and get your key and secret.

You will submit the above form after that you will be get google reCaptcha secret_site and secret_key.

Create Controller
To continue, we’ll need to create a new controller in the application/controllers/ directory called “Google.php.” In this controller. We will build some of the methods like :
- Index() – This is used to show the google recaptcha form
- Store() – Store method is used to store validate data into database
- Put key – In this controller you will put also “ENTER_YOUR_SECRET_KEY” .
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Google extends CI_Controller {
public function __construct() {
parent::__construct();
// load model
$this->load->helper(array('url','form'));
$this->load->library('session');
$this->load->database();
}
public function index(){
$this->load->view('google_recaptcha');
}
public function googleCaptachStore(){
$data = array('name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'mobile_number' => $this->input->post('mobile_number'),
);
$recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
$userIp=$this->input->ip_address();
$secret='ENTER_YOUR_SECRET_KEY';
$credential = array(
'secret' => $secret,
'response' => $this->input->post('g-recaptcha-response')
);
$verify = curl_init();
curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($credential));
curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($verify);
$status= json_decode($response, true);
if($status['success']){
$this->db->insert('users',$data);
$this->session->set_flashdata('message', 'Google Recaptcha Successful');
}else{
$this->session->set_flashdata('message', 'Sorry Google Recaptcha Unsuccessful!!');
}
redirect(base_url('google'));
}
}
?>
Create Views
Now we need to create google_catpcha.php, go to application/views/ folder and create google_catpcha .php file. Here put the below html code into google_recaptcha.php file.
We will also put the YOUR_SITE_KEY in this file, like this ==>
<div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title>Codeigniter Google Recaptcha Form Validation Example - torqueprogramming.co.in</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"/>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="https://www.google.com/recaptcha/api.js"></script>
<style>
.error {
color: red;
}
</style>
</head>
<body>
<div class="container">
<div class="error">
<strong><?=$this->session->flashdata('message')?></strong>
</div>
<br />
<br />
<div class="row">
<div class="col-md-9">
<form method="post" action="<?php echo base_url('google/googleCaptachStore') ?>">
<div class="form-group">
<label for="formGroupExampleInput">Name</label>
<input type="text" name="name" class="form-control" id="formGroupExampleInput" placeholder="Please enter name" />
</div>
<div class="form-group">
<label for="email">Email Id</label>
<input type="text" name="email" class="form-control" id="email" placeholder="Please enter email id" />
</div>
<div class="form-group">
<label for="mobile_number">Mobile Number</label>
<input type="text" name="mobile_number" class="form-control" id="mobile_number" placeholder="Please enter mobile number" maxlength="10" />
</div>
<div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>
<div class="form-group">
<button type="submit" id="send_form" class="btn btn-success">Submit</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
In this Script, we are trying to show how to protect our website, Login Form, Registration Form etc.
Start Development server
For start development server, Go to the browser and hit below the url.
http://localhost/demo/google
Conclusion
In this tutorial for validating Google reCaptcha in CodeIgniter, we’ve created a form to display the captcha validation. We’ve also called the Google API to validate the captcha. After successfully validating the Google captcha, we’ll move on to the next step.
You Also Like This Links:-
Implement Google Column Chart With PHP Codeigniter
Codeigniter Send Email With Gmail Smtp Protocol
Codeigniter Pagination Library Example

