I'm working on session in codeigniter now and I'm not sure where it goes wrong. I only manage to read session for the 1st page. When I browse to other page, the session gone. The following are the var_dump for dashboard.
array(2) {
["__ci_last_regenerate"]=>
int(1487063353)
["logged_in"]=>
array(2) {
["userName"]=>
string(5) "julie"
["userRole"]=>
string(1) "1"
}}
and the following are var_dump for the second page and when i go back to dashboard
array(1) {
["__ci_last_regenerate"]=>
int(1487063602)
}
The following are my codes. Appreciate if you can help me point out the problem and solutions. Thanks in advance.
config/config.php
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 1800; // 30 minutes
$config['sess_save_path'] = BASEPATH .'/sessions/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
libraries/SimpleLoginSecure.php
function login($user_name,$user_pass)
{
$this->CI =& get_instance();
// verification code goes here
//set session data
$user_data = $query->row_array();
$dataSession['userName'] = $user_data['userName'];
$dataSession['userRole'] = $user_data['userRole'];
$this->CI->session->set_userdata('logged_in',$dataSession);
return TRUE;
}
controller/Login.php
function __construct() {
parent::__construct();
}
public function index() {
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<p class="text-red">', '</p>');
$this->form_validation->set_rules('userName', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('userPass', 'Password', 'trim|required|xss_clean|callback_check_database');
if ($this->form_validation->run() == false) {
$this->load->view('index');
}
}
function verify_user()
{
$this->load->library('form_validation');
$this->load->library('SimpleLoginSecure');
$hasher = new PasswordHash(8, false);
$userpass = $this->input->post('inputPassword');
$username = $this->input->post('inputName');
$results = $this->simpleloginsecure->login($username,$userpass);
if($results != FALSE) {
$SESS_data = $this->session->userdata('logged_in');
$data['SESS_username'] = $SESS_data['userName'];
$data['SESS_userrole'] = $SESS_data['userRole'];
var_dump($this->session->all_userdata());
$this->load->view('dashboard', $data);
}
else {
$attempt = $this->simpleloginsecure->updatePassAttempt($userName);
if ($attempt != FALSE) {
echo '<script>alert("Invalid Username or password"); window.history.back();</script>';
}
}
}
function logout() {
$this->load->library('SimpleLoginSecure');
// Logout
$this->simpleloginsecure->logout();
echo '<script>alert("Successfully Logout"); </script>';
redirect(base_url());
}