I'm totally stuck here, I created a controller login which it get, set rules and authentication with DB and at the end of code it set session. Also I have another controller dashboard which displays dashboard and also must use session to see if session is set, if true user can use the controller else redirect to login page. that's it But I can't get session in dashboard, what should I do here?
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller
{
/**
* Login constructor.
*/
public function __construct()
{
parent::__construct();
//Load Form helper
$this->load->helper('form');
//Load form validation library
$this->load->library('form_validation');
}
/**
*Display Login page
*/
public function index()
{
$title['title'] = 'Admin login';
$this->load->view('login', $title);
}
/**
*
* Set rules for authentication
*
* @return bool
*/
public function rules()
{
$rules = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'trim|required|xss_clean'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required|xss_clean'
),
array(
'field' => 'remember',
'label' => 'Remember',
'rules' => 'trim|int|xss_clean'
)
);
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() != FALSE) {
$this->authentication();
} else {
$title['title'] = 'Admin login';
$this->load->view('login', $title);
}
}
/**
* Check if user is exist
*/
public function authentication()
{
$data = array(
'username' => $this->input->post('username', true),
'password' => $this->input->post('password', true)
);
$this->load->model('userData');
if ($this->userData->getUserData($data) == FALSE) {
$title['title'] = 'Admin login';
$this->load->view('login', $title);
} else {
$this->set_session($data);
redirect('dashboard/index');
}
}
/**
* @param $data username|password
*/
public function set_session($data)
{
$session_data = array(
'username' => $data['username'],
'login' => true
);
$this->session->set_userdata('auth', $session_data);
}
}
and here is dashboard:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dashboard extends CI_Controller{
public function __construct()
{
parent::__construct();
}
/**
* Display dashboard
*/
public function index(){
$array = $this->session->userdata('auth');
$login = $array['login'];
if ($login == true) {
$title['title'] = 'Dashboard';
$this->template->load('panel/dashboard', $title);
}else{
redirect('login/index');
}
}
}