Codeigniter Google Recaptcha Form Validation Example – onlinecode
In this post we will give you information about Codeigniter Google Recaptcha Form Validation Example – onlinecode. Hear we will give you detail about Codeigniter Google Recaptcha Form Validation Example – onlinecodeAnd how to use it also give you demo for it if it is necessary.
In this tutorial, we are going to create the Codeigniter Google Recaptcha Form Validation Example. normally captcha is used for security purpose and only human users can pass through. computers or bots are not solving a captcha.
Now we will learn about captcha that how to integrate the Google reCAPTCHA in Codeigniter. so you can see below google captcha code form Validation with Codeigniter.
Overview
Step 1: Create a Database in tableStep 2: Connect to DatabaseStep 3: Get and Set Google Captcha SecretsStep 4: Create ControllerStep 5: Create View File
Step 1: Create a Database in tableIn this step, we have to create a table in the database, so we will create a database using the below code.
CREATE TABLE IF NOT EXISTS 'register' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'first_name' varchar(64) NOT NULL,
'last_name' varchar(64) NOT NULL,
'address' text NOT NULL,
'email' varchar(64) NOT NULL,
'mobile' varchar(12) NOT NULL,
PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
Step 2: Connect to DatabaseGo to the config folder and open database.php file some changes in this file like hostname, database username, database password, and database name.
$db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'enter here database name', '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 );
Step 3: Get and Set Google Captcha SecretsFirst, we need to Google reCAPTCHA site key and secret key. so first we go to Google reCAPTCHA panel and get site key and secret key.
After getting the key, we will create a form using the bootstrap and pass the site key.
When the user fills the form data with resolves the reCAPTCHA code that time response token will be returned.
Step 4: Create ControllerIn this step, we will create a Googlerecaptcha.php file in the “application/controller” directory and paste the below code in this controller.application/controller/Googlerecaptcha.php
<?php
class Googlerecaptcha extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('url','form'));
$this->load->library('session');
$this->load->database();
}
public function index()
{
$this->load->view('google_recaptcha_form');
}
public function googleForm()
{
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'address' => $this->input->post('address'),
'email' => $this->input->post('email'),
'mobile' => $this->input->post('mobile')
);
$recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
$userIp=$this->input->ip_address();
$secret='ENTER_YOUR_SECRET_KEY';
$response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$recaptchaResponse."&remoteip=".$userIp), true);
if($response['success'] == false)
{
$this->session->set_flashdata('message', 'Sorry Google Recaptcha Unsuccessful!!');
}
else
{
echo '<h2>Thanks for posting comment.</h2>';
$this->db->insert('users',$data);
$this->session->set_flashdata('message', 'Google Recaptcha Successful');
}
redirect(base_url('googlerecaptcha'));
}
}
?>
Step 5: Create View FileSo finally, we will create the google_recaptcha_form.php file in the “application/views/” directory and make a form with google Recaptcha code in HTML.application/views/google_recaptcha_form.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Codeigniter Google Recaptcha Form Validation Example - onlinecode</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<div >
<form id="cotact_form" action="<?php echo base_url('googlerecaptcha/googleForm') ?>" method="post">
<div >
<label for="first_name">First Name:</label>
<input type="text" id="first_name" name="first_name">
</div>
<div >
<label for="last_name">Last Name:</label>
<input type="text" id="last_name" name="last_name">
</div>
<div >
<label for="address">Address:</label>
<input type="text" id="address" name="address">
</div>
<div >
<label for="email">Email:</label>
<input type="email" id="email" name="email">
</div>
<div >
<label for="mobile">Mobile:</label>
<input type="text" id="mobile" name="mobile">
</div>
<div >
<div data-sitekey="here_enter_site_key"></div>
</div>
<button type="submit" >Submit</button>
</form>
</div>
</body>
</html>
Hope this code and post will helped you for implement Codeigniter Google Recaptcha Form Validation Example – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs
PHP - I Can't get the $_POST Values on Ajax Request
In this post we will give you information about PHP - I Can't get the $_POST Values on Ajax Request. Hear we will give you detail about PHP - I Can't get the $_POST Values on Ajax RequestAnd how to use it also give you demo for it if it is necessary.
I want to share this posts with you because when i was working on native PHP and Ajax Post request(Specially AngularJS Post request) at that time i can't get Post value on form submit. That's why i would like to share you this post. I also want to tell you i was working on my Ubuntu 14.04. I did try lot but i can't get value.
At last find stuff to how to solve this issue, i did use "file_get_contents('php://input')" that way i could get POST value in json object and i also decode json value using "json_decode()". So let's try this way :
Example:
$post = file_get_contents('php://input');
$post = json_decode($post);
$sql = "INSERT INTO items (title) VALUES ('".$post->title."')";
$result = $mysqli->query($sql);
Hope this code and post will helped you for implement PHP - I Can't get the $_POST Values on Ajax Request. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs