Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{
function __construct()
{
parent:: __construct();
}
public function index($msg = null)
{
$data['msg'] = $msg;
$this->load->view('login_view', $data);
}
public function process()
{
$this->load->model('login_model');
$result = $this->login_model->validate();
if($result)
{
$msg = '<font color=red>Invalid username and/or password.</font><br />';
$this->index($msg);
}
else
{
redirect('home');
}
}
}
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<div id='login_form'>
<form acti echo base_url();?>login/process' method='post' name='process'>
<h2>User Login</h2>
<br />
<?php if(! is_null($msg)) echo $msg;?>
<label for='username'>Username</label>
<input type='text' name='username' id='username' size='25' /><br />
<label for='password'>Password</label>
<input type='password' name='password' id='password' size='25' /><br />
<input type='Submit' value='Login' />
</form>
</div>
</body>
</html>
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
public function validate()
{
$username = $this->security->xss_clean($this->input->post('username'));
$password = $this->security->xss_clean($this->input->post('password'));
$this->db->where('username',$username);
$this->db->where('password',$password);
$query = $this->db->get('employee');
if($query->num_rows == 1)
{
$row = $query->row();
$data = array('id' => $row->id,
'username' => $row->username,
'validated' => true);
$this->session->set_userdata($data);
return true;
}
}
}