I made a login page in codeigniter but i am facing issue that is i am a new on codeigniter and I am not well able to add reChaptcha there but I try to do best but finally it not works. I reads the documentation of google reChaptcha but I not understand that, how i make it functional?
how i make when user login ask for reChaptcha if reChaptcha solve then successful login else error.
my code is below, please help…
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Auth extends Admin_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('model_auth');
}
/*
Check if the login form is submitted, and validates the user credential
If not submitted it redirects to the login page
*/
public function login()
{
$this->logged_in();
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('g-recaptcha-response','Captcha Challenge','required');
$recaptcha_headers = array('Content-Type' => 'application/json');
$recaptcha_fields = array(
'secret' => "MY CODE HERE",
'response' => $this->input->post('g-recaptcha-response'),
'remoteip' => $_SERVER['REMOTE_ADDR']
);
$this->load->library('PHPRequests');
$response = Requests::post("https://www.google.com/recaptcha/api/siteverify", array(), $recaptcha_fields);
$recaptcha_post_body = json_decode($response->body, true);
if ($this->form_validation->run() == TRUE) {
// true case
$email_exists = $this->model_auth->check_email($this->input->post('email'));
if($email_exists == TRUE) {
$login = $this->model_auth->login($this->input->post('email'), $this->input->post('password'));
if($login) {
$logged_in_sess = array(
'id' => $login['id'],
'username' => $login['username'],
'logged_in' => TRUE
);
$this->session->set_userdata($logged_in_sess);
redirect('dashboard', 'refresh');
}
else {
$this->data['errors'] = 'Incorrect username/password combination';
$this->load->view('login', $this->data);
}
}
else {
$this->data['errors'] = 'Email does not exists';
$this->load->view('login', $this->data);
}
}
else {
// false case
$this->load->view('login');
}
}
/*
clears the session and redirects to login page
*/
public function logout()
{
$this->session->sess_destroy();
redirect('auth/login', 'refresh');
}
}
View
<form action="<?php echo base_url('auth/login') ?>" method="post">
<div class="form-group has-feedback">
<input type="email" class="form-control" value="mail@domain.com" name="email" id="email" placeholder="Email" autocomplete="off">
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" value="Asdfghjkl" class="form-control" name="password" id="password" placeholder="Password" autocomplete="off">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="g-recaptcha" data-sitekey="6LfAzH8UAAAAAPq9IBqElP4Z7x6rjc880PbYhaJ_"></div>
<div class="row">
<div class="col-xs-8">
<div class="checkbox icheck">
<label>
<input type="checkbox"> Remember Me
</label>
</div>
</div>
<!-- /.col -->
<div class="col-xs-4">
<button type="submit" class="g-recaptcha btn btn-primary btn-block btn-flat" data-sitekey="my site key here" data-callback="YourOnSubmitFn">Sign In</button>
</div>
<!-- /.col -->
</div>
</form>