I am pretty new to codeigniter. this is my first project i am building a project and i am stuck in my login registration module. Registration functionality is working fine and after login my goal is to redirect the user to admin page where he/she can see his details. but the problem now is after checking the login credential and set the user session with this code
$session_data = array(
'username' => $result[0]->user_name,
'email' => $result[0]->user_email,
);
$this->session->set_userdata('loggedin', $session_data);
redirect('logincontroller/admin_dashboard');
but my session doesn't set. when i var dump the $this->session->userdata['loggedin']
in my admin dashboard view it return empty or does not show anything
Here is the code of my login controller
<?php
class Logincontroller extends CI_Controller
{
public $outputData = array();
public $loggedInUser;
public function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('login_database');
$this->load->helper('url');
$this->load->database();
}
public function index()
{
$this->outputData['pagetitle'] = "Login Form";
$this->load->view('login_form', $this->outputData);
}
public function registrationform()
{
$this->outputData['pagetitle'] = "Registration Form";
$this->load->view('registration_form', $this->outputData);
}
public function user_registration_process()
{
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('email_value', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if ($this->form_validation->run() == FALSE)
{
$this->outputData['pagetitle'] = "Registration Form";
$this->load->view('registration_form', $this->outputData);
}
else
{
$data = array(
'user_name' => $this->input->post('username'),
'user_email' => $this->input->post('email_value'),
'user_password' => $this->input->post('password')
);
$result = $this->login_database->registration_insert($data);
if ($result == TRUE)
{
$this->outputData['pagetitle'] = "Login Form";
$this->outputData['message_display'] = 'Registration Successfully !';
$this->load->view('login_form', $this->outputData);
}
else
{
$this->outputData['message_display'] = 'Username already exist!';
$this->outputData['pagetitle'] = "User Registration";
$this->load->view('registration_form', $this->outputData);
}
}
}
public function login_process()
{
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if ($this->form_validation->run() == FALSE)
{
if(isset($this->session->userdata['loggedin']))
{
redirect('logincontroller/admin_dashboard');
}
else
{
$this->outputData['pagetitle'] = "Login Form";
$this->load->view('login_form', $this->outputData);
}
}
else
{
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
$result = $this->login_database->login($data);
if ($result == TRUE)
{
$username = $this->input->post('username');
$result = $this->login_database->read_user_information($username);
if ($result != false) {
$session_data = array(
'username' => $result[0]->user_name,
'email' => $result[0]->user_email,
);
$this->session->set_userdata('loggedin', $session_data);
redirect('logincontroller/admin_dashboard');
}
}
else
{
$this->outputData['pagetitle'] = "Login Form";
$this->outputData['error_message'] = "Invalid Username or Password";
$this->load->view('login_form', $this->outputData);
}
}
}
public function admin_dashboard()
{
$this->outputData['pagetitle'] = "Admin Dashboard";
$this->load->view('admin_page', $this->outputData);
}
public function logout()
{
$sess_array = array(
'username' => ''
);
$this->session->unset_userdata('loggedin', $sess_array);
$this->outputData['pagetitle'] = "Login Form";
$this->outputData['message_display'] = 'Successfully Logout';
$this->load->view('login_form', $this->outputData);
}
}
I have also used the method specified in this link session destroying in codeigniter after redirecting but nothing happen
I also used php default $_SESSION
in place of $this->session->set_userdata('loggedin', $session_data);
but not worked now i also moved in the site to http website and set the setting in config file also here is the setting of config file
$config['base_url'] = 'http://www.ratneshviop.esy.es/';
$config['index_page'] = 'index.php';
$config['encryption_key'] = '';
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
$config['cookie_prefix'] = 'test';
$config['cookie_domain'] = 'http://ratneshviop.esy.es/';
$config['cookie_path'] = '/';
$config['cookie_secure'] = FALSE;
$config['cookie_httponly'] = FALSE;
$config['csrf_protection'] = FALSE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
$config['csrf_exclude_uris'] = array();
I am using codeigniter 3.0.6 on server with mysql support