1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
/*
* Constructor
*/
function __construct() {
parent::__construct();
//Load required library
$this->load->model('user', '', TRUE);
}
/*
* This function runs when the user navigates directly to the login controller
*/
public function index() {
//If user is already logged in
if($this->user->isLoggedIn()) {
//User already logged in
redirect(base_url().'home', 'refresh');
} else {
//Display the login form
$this->drawLoginForm('');
}
}
/*
* This function validate the user input from login form using the library "form_validation".
* NOTICE: This does NOT mean that it validates it against the database.
*/
public function validate() {
//Load required library
$this->load->library('form_validation');
//Sets validation rules
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
//Run validation
if($this->form_validation->run() == FALSE) {
//Field validation failed. Display login form (with error message).
$this->drawLoginForm(validation_errors());
} else {
$username = $this->input->post('username');
$password = $this->input->post('password');
//Try to login
if ($this->user->login($username, $password)) {
redirect(base_url().'home', 'refresh');
} else {
$this->drawLoginForm('Access denied!');
}
}
}
/*
* This function draws the login form.
*/
private function drawLoginForm($errors) {
//Load required library
$this->load->helper(array('form'));
//Display the view
$data['error'] = $errors;
$headTagData = array(
'cssFiles' => array('header', 'login_view'),
'jsFiles' => array('header', 'login_view', 'userControls')
);
$this->load->view('headTag', array('headTagData' => $headTagData));
$this->load->model('user');
$userInfo = array(
'userType' => $this->user->getUserType(),
'userName' => $this->user->getUserName()
);
$this->load->view('header', $userInfo);
$this->load->view('login_view', $data);
}
}
?>
|