Controller
Create a blank document in the controller file (application -> controller) and name it login.php, in the document add all the following code.<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->library('session');
$this->load->helper(array('form', 'url', 'captcha'));
}
public function index()
{
$this->load->helper('captcha');
$vals = array(
'img_path' => './captcha/',
'img_url' => base_url().'captcha',
'img_width' => 100,
'img_height' => 35
);
$cap = create_captcha($vals);
$data = array(
'captcha_time' => $cap['time'],
'ip_address' => $this->input->ip_address(),
'word' => $cap['word']
);
$this->session->set_userdata($data);
$data['cap_img']=$cap['image'];
$this->load->view('login',$data);
}
public function checkuser()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[3]');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
$this->form_validation->set_rules('captcha', 'Security Code', 'trim|required|callback_check_captcha');
if($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$this->load->model('login_model');
$username = $this->input->post('username');
$password = $this->input->post('password');
$numrow=$this->login_model->checkuserlogin($username,$password);
if($numrow > 0)
{
$this->login_model->loggeduserdata($username,$password);
redirect(base_url().'login/dashboard', 'refresh');
}
}
}
public function check_captcha()
{
$expiration = time()-7200; // Two hour limit
$cap=$this->input->post('captcha');
if($this->session->userdata('word')== $cap
AND $this->session->userdata('ip_address')== $this->input->ip_address()
AND $this->session->userdata('captcha_time')> $expiration)
{
return true;
}
else{
$this->form_validation->set_message('check_captcha', 'Security Code does not match.');
return false;
}
}
public function dashboard()
{
$this->load->view('header');
$this->load->view('sidebar');
$this->load->view('dashboard');
$this->load->view('footer');
}
function logout()
{
$this->session->unset_userdata('logged_in');
$this->session->unset_userdata('userid');
$this->session->unset_userdata('username');
$this->session->unset_userdata('emailid');
$this->session->sess_destroy();
redirect(base_url(), 'refresh');
}
}
?>
View
Create a blank document in the views file (application -> views) and name it login.php, in the document add all the following code.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div class="login-wrap" style="margin-top:120px;margin-bottom:150px;">
<p style="color:#FF0000;font-size:11px;text-align:center;"><?php echo validation_errors(); ?></p>
<h2>Login</h2>
<div class="form">
<?php echo form_open('login/checkuser'); ?>
<p><input type="text" placeholder="Username" name="username" value="<?php echo set_value('username'); ?>" /></p>
<p> <input type="password" placeholder="Password" name="password" /></p>
<p style="text-align:center;">
<?php echo $cap_img; ?>
<INPUT TYPE="text" id="captcha" name="captcha" placeholder="Security Code" style="width:50% !important; margin:0px !important;"></p>
<button type="submit" name="login" value="Submit">Submit</button>
</form>
Model
Create a blank document in the models file (application -> models) and name it login_model.php, in the document add all the following code.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function checkuserlogin($username,$password)
{
$this->db->where("username",$username);
$this->db->where("password",md5($password));
$this->db->where("displayflag",'1');
$query=$this->db->get("dip_admin");
$num=$query->num_rows();
return $num;
}
public function loggeduserdata($username,$password)
{
$this->db->where("username",$username);
$this->db->where("password",md5($password));
$this->db->where("displayflag",'1');
$query=$this->db->get("dip_admin");
foreach($query->result() as $rows)
{
//add all data to session
$newdata = array(
'userid' => $rows->id,
'username' => $rows->username,
'emailid' => $rows->email,
'logged_in' => TRUE
);
}
$session=$this->session->set_userdata($newdata);
return true;
}
public function add_user()
{
$data=array(
'username'=>$this->input->post('user_name'),
'email'=>$this->input->post('email_address'),
'password'=>md5($this->input->post('password'))
);
$this->db->insert('user',$data);
}
}
?>
1 comments:
There is also another method for calling captcha with validation. Here is how: https://www.cloudways.com/blog/captcha-in-codeigniter/
EmoticonEmoticon