I implemented a login system, with session, using CodeIgniter. If the session doesn't exist, redirect to login page. Please review, and let me know what can be done to make it better.
view (login.php)
<body>
<?php echo form_open(base_url('verify'),['id' => 'loginForm', 'name' => 'loginForm', 'method' => 'post'])?>
<div class="login wrap">
<input type="text" name="email" id="email" placeholder="Email" pattern="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$">
<input type="password" name="password" id="password" placeholder="Password">
<!-- <input type="button" value="Help!" /> -->
<input type="submit" value="Log in">
</div>
<?php echo form_close("\n")?>
verify.php (controller)
class Verify extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(['form', 'url', 'security']);
$this->load->library(['form_validation', 'encryption','session']);
$this->load->model('User_Model');
}
public function index()
{
$this->form_validation->set_rules('email','Email','required|trim|xss_clean');
$this->form_validation->set_rules('password','Password','required|trim|xss_clean|callback_check_password');
if ($this->form_validation->run() === FALSE) {
$this->load->view('admin/login');
} else {
redirect(base_url('dashboard/dash'));
}
}
/*login process*/
public function check_password($password)
{
$email = $this->input->input_stream('email', TRUE);
$password = hash('sha256', $password);
$result = $this->User_Model->login($email,$password);
if($result != null){
foreach ($result as $row) {
$sess_data = [
'username' => $row->username,
'user_id' => $row->id
];
$this->session->set_userdata('logged_in', $sess_data);
}
return TRUE;
} else {
$this->form_validation->set_message('check_database', 'invalid username and password');
return FALSE;
}
}
public function logout($page = 'login')
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect(base_url('login'),'refresh');
}
}
Dashboard.php (dashboard controller)
class Dashboard extends Check_Logged
{
public function __construct()
{
parent::__construct();
$this->load->helper(['form', 'url']);
$this->load->library(['form_validation', 'encryption']);
}
public function index()
{
if ($this->logged === TRUE) {
redirect('dashboard/dash');
} else {
$this->load->view('admin/login');
}
}
public function login($page = 'login')
{
if ($this->logged === TRUE) {
redirect('dashboard/dash');
} else {
$this->load->view('admin/'.$page);
}
$this->load->view('admin/'.$page);
}
public function dash($page = 'dashboard')
{
if ($this->logged === TRUE) {
$this->load->view('admin/'.$page);
} else {
redirect('login');
}
}
public function logout($page = 'login')
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect(base_url('login'),'refresh');
}
public function portfolio($page = 'portfolio')
{
if ($this->logged === TRUE) {
$this->load->view('admin/'.$page);
} else {
redirect('login');
}
}
public function team($page = 'team')
{
if ($this->logged === TRUE) {
$this->load->view('admin/'.$page);
} else {
redirect('login');
}
}
}
and check_Logged.php (controller)
class Check_Logged extends CI_Controller
{
public $logged = '';
public function __construct()
{
parent::__construct();
$this->load->helper(['form', 'url', 'security']);
$this->load->library(['session', 'form_validation']);
$this->logged = $this->session_check();
}
/*
* Check the Session status
* if session exist return true
* else return false
* */
public function session_check()
{
if($this->session->userdata('logged_in') != null)
return TRUE;
else
return FALSE;
}
}